变量 = 工作表(=> 预期标识符)

variable = worksheet (=>expected identifier)

首先,抱歉我的英语不好

所以我有一个宏将我的工作簿的工作 sheet 设置为变量名称,以防止在用户使用工作簿并更改 [=42] 的名称或位置时必须调试警报=].

例如,如果我的 sheet 的名字是“BDD”,当我编码时,我将把这个 sheet 称为 Sheets("'BDD").xxxxxx 或 sheets(1).xxxxxxx 那么如果用户更改 sheet 的名称,我的宏将不再工作

这就是我在 worbook_open 中设置的原因:

WB00 = activeworkbook
WB00WS01 = first sheet
WB00WS02 = second sheet

不断

因此,当我编码时,我将其称为 WB00WS01.xxxx

所以,我的宏是:

    Sub initWB00()
        Set WB00 = ActiveWorkbook
        For Each W In WB00.Worksheets
            On Error Resume Next
            If W.CodeName = "Sheet1" Then
                Set WB00WS01 = W
            End If
            If W.CodeName = "Feuil2" Then
                Set WB00WS02 = W
            End If
            If W.CodeName = "Feuil3" Then
                Set WB00WS03 = W
            End If
            If W.CodeName = "Feuil4" Then
                Set WB00WS04 = W
            End If
            If W.CodeName = "Feuil5" Then
                Set WB00WS05 = W
            End If
            If W.CodeName = "Feuil6" Then
                Set WB00WS06 = W
            End If
            If W.CodeName = "Feuil7" Then
                Set WB00WS07 = W
            End If
            If W.CodeName = "Feuil8" Then
                Set WB00WS08 = W
            End If
            If W.CodeName = "Feuil9" Then
                Set WB00WS09 = W
            End If
            If W.CodeName = "Feuil10" Then
                Set WB00WS10 = W
            End If
        Next W
    End Sub

它按原样工作,但如您所见,它非常有限(例如,如果我想升级到 WB00WS100)并且非常琐碎

然后,我有了循环变量“i”的想法,它将给出 sheet "WB00WS" & i = "sheet" & I

的编号
Sub initWB00()
Dim i As Integer
    Set WB00 = ActiveWorkbook
    For Each W In WB00.Worksheets
        For i = 1 To 20
        If W.CodeName = "Feuil" & i Then
            Set "WB00WS0"&i = W
        End If
        Next i
    Next W
End Sub

如果我那样做,就会出现预期的标识符错误

我想这是因为我在等式的左边放了一个字符串和一个变量,但除此之外我无法想象如何编写一个循环来满足我的需要

你有什么想法吗?

提前致谢!!

这是一种使用工作表引用填充数组并模拟您在代码中所做的尝试的方法。不过,Raymond Wu的观点可能是更正确的解决方案。

Option Explicit

Public Function initWB00(ByRef ipWB As Workbook) As Variant
    
    Dim myWorksheetArray As Variant
    ReDim myworksheet(1 To ipWB.Worksheets.Count)
    
    Dim myItem As Variant
    Dim mySheet As Worksheet
    For Each myItem In ipWB.Worksheets
    
        ' The next line just allow intellisense
        Set mySheet = myItem
        If InStr(mySheet.CodeName, "Sheet1") Then
        
            Set myWorksheetArray(1) = mySheet
            
        Else
        
            Set myWorksheetArray(CLng(Split(mySheet.CodeName, "l")(1))) = mySheet
        
        End If
        
    Next
    
    initWB00 = myWorksheetArray
    
End Function

请尝试使用下一种方法。您将 运行 代码一次,所有工作表代码名称都将更改。那么,您只能使用它们:

Sub initWB00()
    Dim i As Long, w As Worksheet, WB00 As Workbook
    
    Set WB00 = ActiveWorkbook 'if the active workbook is not ThisWorkbook, it will change the names of he active workbook sheets.
                              'but they can be used only running codes inside them
                              'In order to not have such problems, you should use ThisWorkbook instead of ActiveWorkbook 
    For Each w In WB00.Worksheets
        For i = 1 To 20
            If w.CodeName = "Feuil" & i Then
                ChangeCodeName w, WB00, "WB00WS0" & i
            End If
        Next i
    Next w
End Sub

Private Sub ChangeCodeName(sh As Worksheet, WB As Workbook, strCodeName As String)
  Dim shCModule As Object
  Set shCModule = WB.VBProject.VBComponents(sh.CodeName)
  shCModule.Name = strCodeName
End Sub

Soooooooooooooooo 感谢@FaneDuru 我找到了解决方案!我只需要在我的代码开头删除全局名称(我有全局 WB00WS01、全局 WB00WS2...)并且它起作用了!

我剩下的唯一问题是在我的代码中某处我将 WB00 设置为 ThisWorkbook 并且我稍后无法在我的代码中引用它。即使我在代码的 header 中将 WB00 调暗为工作簿。当我使用 WB00.Windows(1).VisibleRange(2, 2).Top 我有一个未定义的变量错误

无论如何,感谢大家对我的帮助!!