VBA String Variable Contains Other Variable

你。 提交于 2020-03-05 00:33:20

问题


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 ?

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

The text file has this line:

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

I am calling the function

MessageBody = getAdmSetting("email_body") 

which returns the string exactly:

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

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. 

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


回答1:


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.

In your text file:

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

then you can

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


来源:https://stackoverflow.com/questions/60400083/vba-string-variable-contains-other-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!