vba VBA如何将字符串作为一行代码运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24956527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA how to run a string as a line of code
提问by charles_m80
Is there a way to convert a string into an executable line of code?
有没有办法将字符串转换为可执行的代码行?
something like:
就像是:
Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1
resulting in the pop up box saying Hello.
导致弹出框说你好。
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
or make the variable an array and store it in element 1 e.g. and use
或将变量设为数组并将其存储在元素 1 中,例如并使用
Application.Run Line1(1)
but it doesn't work for me.
但这对我不起作用。
Ok, while writing this I've also been experimenting. I found that
好的,在写这篇文章的同时,我也一直在试验。我找到
Eval (Line1)
will work when Line1 is a message box, but not when it is something like:
当 Line1 是一个消息框时会起作用,但当它是这样的时候不起作用:
line1 = "DoCmd.OpenForm ""form1"""
Any tips would be appreciated.
任何提示将不胜感激。
Thanks
谢谢
回答by Jimmy Smith
You can use,
您可以使用,
Eval("DoCmd.OpenForm(""form1"")")
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
您必须确保包含的任何函数都使用括号。进一步参考, http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx
回答by Nerobyrne
Visual Basic is a compiler language, and as such does not support the ability to execute human-readable code while it is running. All code written in VBA must first be compiled BEFORE the program runs the first time. However, SQL is an interpreter language, and can be handed code which it will execute line by line. You can also fetch contents for variables from other sources while the program runs, you just can't build a string while the program is running and then execute it in VBA directly.
Visual Basic 是一种编译器语言,因此不支持在运行时执行人类可读代码的能力。在程序第一次运行之前,必须先编译所有用 VBA 编写的代码。但是,SQL 是一种解释器语言,可以将代码逐行执行。您还可以在程序运行时从其他来源获取变量的内容,只是不能在程序运行时构建字符串,然后直接在 VBA 中执行它。
回答by Anthony Griggs
You didn't have to go through such extremes... as you are not actually dynamically executing code. The Controls() Property accepts a test string so you could have used any source including a much simpler table, or array.
您不必经历这种极端情况……因为您实际上并不是在动态执行代码。Controls() 属性接受测试字符串,因此您可以使用任何源,包括更简单的表或数组。
The same thing could have been accomplished as in your initial example like so:
可以像在您的初始示例中那样完成相同的事情,如下所示:
Dim Line1 as String
Line1 = "Hello"
MsgBox Line1
回答by charles_m80
It's not exactly what I was asking, I ended up going a slightly different direction, but here's what I ended up doing and it would probably be easily altered to more closely match my question. I actually took lines of text from an external text file and inserted them into a line of code. What I was doing in this example was just hiding columns, the external text file was a list of column names. (figuring out how to output that was fun too)
这不完全是我要问的,我最终走向了一个稍微不同的方向,但这就是我最终做的事情,它可能很容易改变以更接近我的问题。我实际上从外部文本文件中取出了几行文本并将它们插入到一行代码中。我在这个例子中所做的只是隐藏列,外部文本文件是一个列名列表。(弄清楚如何输出也很有趣)
Open "C:\UserList.txt" For Input As #TextFile
While Not EOF(TextFile)
Line Input #TextFile, TextLine
Screen.ActiveDatasheet.Controls(TextLine).ColumnHidden = True
Wend