VBA字符串变量包含其他变量 [英] VBA String Variable Contains Other Variable

查看:150
本文介绍了VBA字符串变量包含其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用文本和其他变量构建的字符串.
有没有一种方法可以构建字符串,以便可以使用其他变量?

I have a string that I am trying to build with text and other variables.
Is there a way to build the string so that I can use other variables ?

我有一个文本文件,我的vba正在阅读该文本文件来组成电子邮件的正文.

I have a text file that my vba is reading to compose the body of an email.

文本文件具有以下行:

email_body|"Final Reports will be distributed on or after \" & Me.cbo_Month.Column(0) & \" 10th. 

我正在调用函数

MessageBody = getAdmSetting("email_body") 

它完全返回字符串:

Final Reports will be distributed on or after \" & Me.cbo_Month.Column(0) & \" 10th.

如果我调试.print MessageBody,我将得到正确的字符串.我正在尝试获取要打印的Me.cbo_Month.Column(0)的值.

If I debug.print MessageBody, i get the string exactly. I am trying to get the value of Me.cbo_Month.Column(0) to print.

debug: Final Reports will be distributed on or after \" & Me.cbo_Month.Column(0) & \" 10th. 

我要打印: 最终报告将于1月10日或之后分发.

I would like it to print: Final Reports will be distributed on or after January 10th.

推荐答案

您无法像使用其他某些语言一样轻松地在VBA中评估随机代码.使用替换的令牌会更好.

You can't easily evaluate random pieces of code in VBA like you can in some other languages. You'd be better off using tokens which you replace.

在您的文本文件中:

email_body|Final Reports will be distributed on or after {month} 10th.

那么您就可以

Debug.Print Replace(MessageBody, "{month}", Me.cbo_Month.Column(0))

这篇关于VBA字符串变量包含其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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