【问题标题】:"Compile error: Duplicate declaration in current scope" for a Worksheet Change event工作表更改事件的“编译错误:当前范围内的重复声明”
【发布时间】: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


【解决方案1】:

您最好重构以删除重复:

Private Sub Worksheet_Change(ByVal Target As Range)

    ProcessChange Target, Range("B30"), _
          "*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*"
          
    ProcessChange Target, Range("B36"), _
          "*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*"

End Sub

Sub ProcessChange(Target As Range, KeyCells As Range, SheetsList As String)
    Dim ColNum As Long, v, ws As Worksheet
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        v = KeyCells.Value
        If IsNumeric(v) Then
            ColNum = v
            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(SheetsList), LCase("*" & ws.Name & "*"))) Then
                            InsertColumnsOnSheet argSheet:=ws, argColNum:=ColNum
                        End If
                    End If
                Next ws
            End If
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多