VBA如何将字符串作为一行代码运行 [英] VBA how to run a string as a line of code
问题描述
是否可以将字符串转换为可执行的代码行?
Is there a way to convert a string into an executable line of code?
类似以下内容:
Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1
在弹出的框中说你好。
我正在做的是从文本文件。我可以拉出作为表单控件名称的行,并使用它们对这些表单控件执行操作,但我想更进一步并执行行。
我见过这样的说法:
What I'm doing is pulling lines out of a text file. I can pull lines that are the names of form controls and use them to perform actions on those form controls but I'd like to go one step further and execute lines. I've seen claims that this would work:
Application.Run Line1
或将变量设为数组并将其存储在元素1中,例如并使用
or make the variable an array and store it in element 1 e.g. and use
Application.Run Line1(1)
但对我来说不起作用。
好,在编写此代码时,我也在做实验。我发现
Ok, while writing this I've also been experimenting. I found that
Eval (Line1)
$当Line1是一个消息框时,b
$ b
将起作用,但在诸如以下消息时则无效:
will work when Line1 is a message box, but not when it is something like:
line1 = "DoCmd.OpenForm ""form1"""
任何提示都将不胜感激。
Any tips would be appreciated.
谢谢
推荐答案
您可以使用,
Eval("DoCmd.OpenForm(""form1"")")
必须确保所包含的任何功能都使用括号。
进一步参考,
http://msdn.microsoft.com/zh-CN/library/office/aa172212(v = office.11).aspx
You have to make sure any functions you include use parentheses. Further reference, http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx
这篇关于VBA如何将字符串作为一行代码运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!