我在 Excel 2016 VBA module 中有多个函数/程序引用名为 DataSheet
的 CodeName .
Public Sub LoadRecords()
'***Caution: The 'DataSheet' CodeName is hardcoded (in multiple places).***
With DataSheet.ListObjects(tblName)
'Some code
Next
End With
End Sub
Function ConcatVars(RowNum As Integer) As String
'***Caution: The 'DataSheet' CodeName is hardcoded (in multiple places).***
For Each Column In DataSheet.ListObjects(tblName).ListColumns
'Some code
Next
End Function
问题:有没有办法将 CodeName 声明为常量?
我试过的:
如果我将 codeName 放在一个常量中:
Public Const codeName = "DataSheet"
并尝试引用常量:With codeName.ListObjects(tblName)
我收到此错误:Compile Error: Invalid qualifier
最佳答案
只需定义一个函数(并避免使用相同的函数或变量名重载现有的 CodeName 属性):
Function myCodeName() As Worksheet
Set myCodeName = DataSheet ' << change to your project's Code(Name)
'Debug.Print myCodeName.CodeName
End Function
关于excel - 代号为常数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62583341/