将常量的字符串表示形式转换为常量? [英] Converting a string representation of a constant into a constant?

查看:407
本文介绍了将常量的字符串表示形式转换为常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据单元格接受格式常数,所以我有一个字符串xlTotalsCalculationAverage。如何将其转换为它所代表的Excel常量?常量 xlTotalsCalculationAverage 等于整数2。

I'm trying to accept a formatting constant from a data cell, so I have a string "xlTotalsCalculationAverage". How can I translate that into the Excel constant it represents; the constant xlTotalsCalculationAverage is equal to the integer 2.

ActiveSheet.ListObjects(1).ListColumns(RemainingHeader).TotalsCalculation = xlTotalsCalculationAverage

是静态表示。

is a static representation.

TargetTotal = something("xlTotalsCalculationAverage")
ActiveSheet.ListObjects(1).ListColumns(RemainingHeader).TotalsCalculation = TargetTotal

是我的目标。

我可以做一个巨大的case或switch语句,但是重复所有可能的值似乎很愚蠢。

I could make a huge case or switch statement, but it seem silly to duplicate all the possible values.

如何使Excel将此字符串转换为已知的常量值?

How can I make Excel convert this string to a known constant value?

推荐答案

总是这样的:

Sub Tester()
    MsgBox WhatIsTheValue("xlTotalsCalculationAverage")
    MsgBox WhatIsTheValue("xlTotalsCalculationCountNums")
End Sub



Function WhatIsTheValue(s As String) As Variant

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("modTemp")
        Set CodeMod = VBComp.CodeModule

        With CodeMod
            .DeleteLines 1, .CountOfLines
            .InsertLines 1, "Public Function GetEnumVal()"
            .InsertLines 2, "GetEnumVal = " & s
            .InsertLines 3, "End Function"
        End With
        WhatIsTheValue = Application.Run("GetEnumVal")

End Function

但真的 - 不要这样做。

But really - don't do that.

这篇关于将常量的字符串表示形式转换为常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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