【发布时间】:2021-12-07 13:22:17
【问题描述】:
我目前有以下代码,允许我根据某个值在我指定的工作表上插入列:
Private Sub Worksheet_Change(ByVal Target As Range)
Const SOMESHEETS As String = "*MemberInfo-20*C-Proposal-20*Schedule J-20*NOL-20*Schedule R-20*NOL-P-20*SchA-3-20*Schedule H-20*NOL-PA-20*Schedule A-20*Schedule A-5-20*C-Proposal-19*MemberInfo-19*Schedule J-19*NOL-19*NOL-P-19*NOL-PA-19*Schedule R-19*Schedule A-3-19*Schedule A-19*Schedule H-19*"
Dim KeyCells As Range, ColNum As Long
Dim ws As Worksheet
Set KeyCells = Range("B30")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
If IsNumeric(KeyCells.Value) Then
ColNum = KeyCells.Value
If ColNum > 0 Then
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then 'Skip this Sheet and process next sheet
If CBool(InStr(LCase(SOMESHEETS), LCase("*" & ws.Name & "*"))) Then
InsertColumnsOnSheet argSheet:=ws, argColNum:=ColNum
End If
End If
Next ws
End If
End If
End If
End Sub
我遇到的问题是,每张以 -19 列出的工作表都需要链接到列出的 B30 的 KeyCells 范围。每张带有 -20 的工作表都需要有一个 B36 的 KeyCells Range。
以下代码提供了编译错误。我理解为什么它会抛出这个错误,但无法找到解决方法
Private Sub Worksheet_Change(ByVal Target As Range)
Const SOMESHEETS As String = "*C-Proposal-19*MemberInfo-19*Schedule J-19*NOL-19*NOL-P-19*NOL-PA-19*Schedule R-19*Schedule A-3-19*Schedule A-19*Schedule H-19*" ' <<< change / append sheet names to suit
' be sure each sheet name is between * characters
Dim KeyCells As Range, ColNum As Long
Dim ws As Worksheet
Set KeyCells = Range("B30")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
If IsNumeric(KeyCells.Value) Then
ColNum = KeyCells.Value
If ColNum > 0 Then
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then 'Skip this Sheet and process next sheet
If CBool(InStr(LCase(SOMESHEETS), LCase("*" & ws.Name & "*"))) Then
InsertColumnsOnSheet argSheet:=ws, argColNum:=ColNum
End If
End If
Next ws
End If
End If
End If
Const SOMESHEETS As String = "*MemberInfo-20*C-Proposal-20*Schedule J-20*NOL-20*Schedule R-20*NOL-P-20*SchA-3-20*Schedule H-20*NOL-PA-20*Schedule A-20*Schedule A-5-20*" ' <<< change / append sheet names to suit
' be sure each sheet name is between * characters
Set KeyCells = Range("B36")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
If IsNumeric(KeyCells.Value) Then
ColNum = KeyCells.Value
If ColNum > 0 Then
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then 'Skip this Sheet and process next sheet
If CBool(InStr(LCase(SOMESHEETS), LCase("*" & ws.Name & "*"))) Then
InsertColumnsOnSheet argSheet:=ws, argColNum:=ColNum
End If
End If
Next ws
End If
End If
End If
结束子
【问题讨论】:
-
你定义了两次
SOMESHEETS -
我知道 SOMESHEETS 被定义了两次。我不确定是否能够拆分工作表以应用两个不同的 keyCell 值。
-
可以,但您需要使用
din somesheets as string,然后在if中设置一些表格
标签: excel vba compiler-errors