是否有一个VBA函数需要一个内置常量的名称并返回相应的值?
我正在编写一个宏来自动设置某些单元格区域的格式。为了使它尽可能多用,我想通过输入字符串在运行时做出一些格式化决定 目前我使用select case语句,这对我来说似乎非常乏味是否有一个VBA函数需要一个内置常量的名称并返回相应的值?,vba,function,constants,Vba,Function,Constants,我正在编写一个宏来自动设置某些单元格区域的格式。为了使它尽可能多用,我想通过输入字符串在运行时做出一些格式化决定 目前我使用select case语句,这对我来说似乎非常乏味 Dim strInput As String strInput = "xlDashDotDot" Dim nConstVal As Integer Select Case strInput Case "xlContinuous" nConstVal = xlContinuous Cas
Dim strInput As String
strInput = "xlDashDotDot"
Dim nConstVal As Integer
Select Case strInput
Case "xlContinuous"
nConstVal = xlContinuous
Case "xlDash"
nConstVal = xlDash
Case "xlDashDot"
nConstVal = xlDashDot
Case "xlDashDotDot"
nConstVal = xlDashDotDot
Case "xlDot"
nConstVal = xlDot
Case "xlDouble"
nConstVal = xlDouble
Case "xlLineStyleNone"
nConstVal = xlLineStyleNone
Case "xlSlantDashDot"
nConstVal = xlSlantDashDot
End Select
[...]
rng.Borders.LineStyle = nConstVal
是否有一个函数采用内置常量的名称并返回相应的值
Dim strInput As String
strInput = "xlDashDotDot"
Dim nConstVal As Integer
nConstVal = GetConstVal(strInput)
工具-参考-
然后:
选项显式
'在模块级别缓存此变量,以避免在每次调用时重新加载它
私有mLib作为TLI.TypeLibInfo
私有函数EnumValueFromString(ByVal常量作为TLI.Constants,ByVal EnumName作为String,ByVal EnumValueName作为String)作为变量
EnumValueFromString=Constants.NamedItem(EnumName).GetMember(EnumValueName).Value
端函数
子测试()
使用新的TLI.TLI应用程序
设置mLib=.TypeLibInfoFromFile(Application.Path&“\EXCEL.EXE”)
以
调试。打印EnumValueFromString(mLib.Constants、“XlLineStyle”、“xlDashDot”)
调试。打印EnumValueFromString(mLib.Constants、“XlLineStyle”、“xlDashDotDot”)
调试。打印EnumValueFromString(mLib.Constants,“XlLineStyle”,“xlDot”)
端接头
?您想解决什么问题?您是否试图获取命名常量的基础值以用于文档编制?这已经在docs.microsoft.com上了;所有这些常量都可以在对象浏览器(F2)中找到,并且标识符名称上的Ctrl+i应该为您提供一个带有基础值的工具提示。在什么情况下,您要处理一个包含编程标识符名称的字符串值?正如@GSerg所提到的,最有可能的事情是您解决了错误的问题。假设实际的strInput
来自包含格式元数据的工作表,更好的方法是让工作表为您提供实际的基础值,而不是名称。您可以有一个查找表,使其更加用户友好。归根结底,这是数据,而不是代码-数据不属于代码,这就是为什么它很乏味=)无论如何,答案是不,没有这样的函数。是的,如果你把手伸进VBA的内脏。。。不完全是OP想要的那种“内置的”,我肯定=)@MathieuGuindon啊,是的,内置的。