如何为VBA中的运行时提供属性名称的属性赋值? [英] How do I assign a value to a property where the property name is supplied at runtime in VBA?

查看:252
本文介绍了如何为VBA中的运行时提供属性名称的属性赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个工作表,创建一个用于初始化实例化类的值的值列表。

I'm trying to create a worksheet that creates a list of values that will be used to initialize the values of an instantiated class.

例如,我可能在我的初始化工作表中有以下内容:

For example, I might have the following in my initialization worksheet:

Property Name              Value
StartingCol                A
StartingRow                11

然后,我将创建一个解析此工作表的类,并为我提供一个可用于初始化实例化对象的属性。但是,我不知道如何在运行时使用字符串指定属性值,而不是在代码中明确指定它。您可以在下面的代码中了解我要完成的任务:

I'd then create a class that would parse this worksheet and provide me with an enumberable that I could use to initialize the properties of an instantiated object. However, I'm not sure how I might be able to specify the property value at runtime using a string rather than specifying it explicitly in code. You can get an idea of what I'm trying to accomplish in the code below:

Sub test_PropertyAssignment()
Dim sp As SheetParser
Dim strFieldName As String
Dim strFieldNameValue As String
Set sp = New SheetParser

'The property name is supplied explicitly'
sp.StartingCol = "B"

strFieldName = "StartingCol"
strFieldNameValue = "B"

sp.[how can I supply strFieldName to specify the property?] = strFieldNameValue 'Will not Work'

End Sub

有没有办法在运行时使用一个字符串来指定属性名称,而不是在代码中明确指定?

Is there a way to use a string at runtime to specify the property name rather than specifying explicitly in code?

推荐答案

查找CallByName函数新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新旗您应该可以执行以下操作:

Look up the CallByName function in the VBA help. You should be able to do something like:


调用CallByName(sp,strFieldName,vbLet,strFieldNameValue)

这篇关于如何为VBA中的运行时提供属性名称的属性赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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