vba - 是否有一个 VBA 函数需要内置常量的名称并返回相应的值?
问题描述
我正在编写一个宏来自动格式化某些单元格范围。为了使其尽可能通用,我想通过输入字符串在运行时做出一些格式化决定。
目前我使用 select-case-statement,这对我来说似乎很乏味。
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)
解决方案
工具 - 参考资料 - TypeLib Information
。
然后:
Option Explicit
' Cache this variable on module level to not reload it on each call
Private mLib As TLI.TypeLibInfo
Private Function EnumValueFromString(ByVal Constants As TLI.Constants, ByVal EnumName As String, ByVal EnumValueName As String) As Variant
EnumValueFromString = Constants.NamedItem(EnumName).GetMember(EnumValueName).Value
End Function
Sub Test()
With New TLI.TLIApplication
Set mLib = .TypeLibInfoFromFile(Application.Path & "\EXCEL.EXE")
End With
Debug.Print EnumValueFromString(mLib.Constants, "XlLineStyle", "xlDashDot")
Debug.Print EnumValueFromString(mLib.Constants, "XlLineStyle", "xlDashDotDot")
Debug.Print EnumValueFromString(mLib.Constants, "XlLineStyle", "xlDot")
End Sub
推荐阅读
- javascript - 解决无法集中读取未定义的属性“propertyName”
- sql - 在VB6中我们需要使用哪些adodb记录集选项来在短时间内从SQL中加载数据
- sql - 将记录链接到其他人
- c# - 如何用星号打印星号图案
- ios - 如何在点击的光线触摸而不是来自中心视图的光线处生成 Reality Composer 场景
- apache - 确定原因并修复 302 重定向
- word2vec - 为什么“[UNK]”这个词在 word2vec 词汇表中排在第一位?
- typescript - 打字稿(tsc)显示编译时间
- reactjs - 如何在反应多轮播中居中图像?
- reactjs - 类型错误:Object(...) 不是函数 React js