我可以评估字符串格式的Excel VB常量吗? [英] Can I Evaluate An Excel VB Constant That Is In String Format?

查看:181
本文介绍了我可以评估字符串格式的Excel VB常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以评估一个包含有效的Excel VB常数的名称
的字符串来返回该常量的值?

Is it possible to Evaluate a String which contains a valid Excel VB Constant's Name to return that Constant's Value?

例如

    Dim ConstantName as String
    Dim ConstantValue as Long

    ConstantName="xlValues"

    ConstantValue= UnknownFunction(ConstantName)

    'would set ConstantValue=-4163


推荐答案

有趣!

Option Explicit

Function getConstantValue(constStr As String) As Variant

    Dim oMod As VBIDE.CodeModule
    Dim i As Long, _
        num As Long

    Set oMod = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule

    For i = 1 To oMod.CountOfLines
        If oMod.Lines(i, 1) = "Function tempGetConstValue() As Variant" Then
            num = i + 1
            Exit For
        End If
    Next i

    oMod.InsertLines num, "tempGetConstValue = " & constStr

    getConstantValue = Application.Run("tempGetConstValue")

    oMod.DeleteLines num

End Function

Function tempGetConstValue() As Variant
End Function

所有代码必须位于名为Module1的模块中。这可以通过在例程中更改文本Module1来更改。

All code must be in a module called Module1. That can be changed pretty simply by changing the text "Module1" in the routine.

您需要添加对 Microsoft Visual Basic的引用对于应用程序可扩展性xx

有多种方法可能会失败。如果您有任何问题,请告诉我们:)

There are a number of ways this could fail. Let me know if you have any problems with it :)

这篇关于我可以评估字符串格式的Excel VB常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆