本文介绍了如何在VBA中使用字符串获取对变量的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量strFunction,然后还有另一个字符串strName = "strFunction",我想知道的是如何使用strName获得strFunction的值.

I have a variable strFunction, then I have another string strName = "strFunction" , what I want to know is how can I get the value of strFunction by using strName.

例如,类似getValue(strName)的东西为我提供了strFunction的值.在Access VBA中可以吗?

For example, something like getValue(strName) gives me the value of strFunction. Is it possible in Access VBA?

谢谢!

我有一个strFunction字符串,它是一个const字符串.在我的代码中,我想使用Len("strFunction")来测试它的长度,但是我得到的是长度"strFunction".因此,我需要一个变量值获取值功能.我已经尝试过Eval(),但是即使我写了get_strFunction(),它也无法做到这一点,eval("get_strFunction()")给了我错误,告诉我找不到它.

I have a strFunction string, it's a const string.In my code I want to use Len("strFunction") to test the length of it, but what i got is the length "strFunction". So I need a get-value-out-of-variable-name function. I have tried Eval(), but it cannot do this, even I write a get_strFunction(), eval("get_strFunction()") gives me error, telling me it cannot find it.

Private Const strFunction as String = "FilterByType_1"
Private Function get_strFunction()
    get_strFunction = strFunction
End Function

推荐答案

"我有一个变量strFunction,然后我有另一个字符串strName =" strFunction,我想知道如何获取该值通过使用strName来实现strFunction."

strFunction 可能不是变量,而是VBA集合中某个项目的键.

Instead of a variable, strFunction could be the key for an item in a VBA collection.

Public Sub darkjh()
    Dim strName As String
    Dim col As Collection
    Set col = New Collection
    col.Add "FilterByType_1", "strFunction"
    strName = "strFunction"
    Debug.Print col(strName)
    Set col = Nothing
End Sub

编辑:您可以使用Scripting.Dictionary代替VBA集合.

Edit: Instead of a VBA collection, you could use a Scripting.Dictionary.

Dim strName As String
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "strFunction", "FilterByType_1"
strName = "strFunction"
Debug.Print dict(strName)
Set dict = Nothing

这篇关于如何在VBA中使用字符串获取对变量的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:52