VBA读取单元格值作为variables定义,而不是文本

我的同事们经常不得不发出邀请面试(一次多达几十个)。 我正在编写一个macros,帮助他们根据所有被访者的信息自动生成Outlook会议邀请,这些信息存储在Excel数据文件中。

例如:“亲爱的[插入姓名],我们想邀请您在[插入date]接受关于[INSERT SUBJECT]的采访,请使用[插入电话号码]进入采访,并准备讨论[INSERT条件]。”

因为我的同事不知道VBA,我试图让他们把它写在input表中,并让程序读取格式化,并将其存储在名为MeetingInviteBody的variables中。 所以我需要把单元格的值作为一个variables定义来读取它。 问题是,整个事情是作为一个stringinput,即使单元格的内容是部分string和部分引用另一个variables。 有没有办法来解决这个问题?

先谢谢你!

附录:我希望这将澄清我正在尝试做什么。 这是我写作的一个testingmacros:

Sub MultipleDataTypeInput() Dim FirstLineofText As Variant Dim PhysicianName As String PhysicianName="Dr. Smith" FirstLineofText=Sheets("Sheet1").Cells(1,1).Value MsgBox(Prompt:=FirstLineofText) End Sub 

我在单元格A1中input“Dear”&PhysicianName&“,”,我希望Excel能够读取macros
FirstLineofText =“Dear”&PhysicianName&“,”如果发生这种情况,MsgBox会说“亲爱的史密斯博士”,而提示是“亲爱的”&PhysicianName&“,”“

这有道理吗?

一种方法是使用VBA中的replace函数。

但是,在单元格A1中,写下"Dear PhysicianName,"并摆脱您的连接。 (如果你严格按照VBA编写的话,你写的方式是可行的,代码会把值连接在一起,例如: FirstLineofText = "Dear " & PhysicianName & ","

 Sub MultipleDataTypeInput() Dim FirstLineofText As Variant Dim PhysicianName As String PhysicianName="Dr. Smith" FirstLineofText=Sheets("Sheet1").Cells(1,1).Value FirstLineofText = Replace(FirstLineOfText,"PhysicianName",PhysicianName) MsgBox(Prompt:=FirstLineofText) End Sub 

我build议你这样做,因为你说同事们正在写自己的脚本,我不想提出一个全新的方法,因为它可能会让你困惑。 这就是说,我认为有更有效的方法来devise这个。

我认为你的逻辑落后了

你可能想要这个

 Sub MultipleDataTypeInput() Dim PhysicianName As String Dim PatientName As String PhysicianName = Sheets("Sheet1").Range("A1").Value ' this cell should hold "Dr. Smith" PatientName = Sheets("Sheet1").Range("B1").Value MsgBox "Dear" & PhysicianName & ", We would like to invite you to an interview about " & PatientName End Sub