如何在 VBA 中格式化文本/字符串?
在下面的代码中,我采用一些输入参数(文本或单元格),并使用我需要的格式将它们组合起来形成一个字符串。我需要将 Task_Name 以及“Lead :”等文本设为粗体。我知道你不能将文本设置为粗体,但我该怎么做呢?我存储值的这个单元格最终会在 Word 邮件合并中使用。
我需要格式化字符串的一部分。在下面的代码中,我需要将Task_Name、“Lead :”等全部加粗。
Function GENERATE_STAFFING_SECTION(Task_Name, Lead_By, Members, Instructions)
Dim tmpSection As String
If Len(Task_Name > 0) And Len(Lead_By) > 0 And Len(Members) > 0 And Len(Instructions) > 0 Then
tmpSection = vbLf _
& Task_Name _
& vbLf & "Lead : " & Lead_By _
& vbLf & "Ambassadors : " & Members _
& vbLf & "Instructions : " & Instructions _
& vbLf
Else
tmpSection = ""
End If
GENERATE_STAFFING_SECTION = tmpSection
End Function
另外,我知道这不是最干净的代码,因此如果有任何其他改进建议,我们非常欢迎。
谢谢!
In the code below, I take some input parameters, either text or a cell, and combine them to form one string using the formatting I need. I need to make Task_Name bold, as well as text like "Lead :". I know you cannot make text in a variable bold, but how do I go about this? This cell I'm storing the value in is eventually used in a Word mail merge.
I need to format part of a string. In the code below, I need to make Task_Name, "Lead : ", etc. all bold.
Function GENERATE_STAFFING_SECTION(Task_Name, Lead_By, Members, Instructions)
Dim tmpSection As String
If Len(Task_Name > 0) And Len(Lead_By) > 0 And Len(Members) > 0 And Len(Instructions) > 0 Then
tmpSection = vbLf _
& Task_Name _
& vbLf & "Lead : " & Lead_By _
& vbLf & "Ambassadors : " & Members _
& vbLf & "Instructions : " & Instructions _
& vbLf
Else
tmpSection = ""
End If
GENERATE_STAFFING_SECTION = tmpSection
End Function
Also, I know it's not the cleanest code, so if there are any other suggestions for improving it, they are most welcome.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能直接向字符串添加任何内容以使单元格具有粗体字符。
将字符串写入单元格后,您需要返回并重新处理单元格。
例如:
此代码片段将仅将单元格的一部分设置为粗体。
编辑:
此代码可用于实现上述内容并为您提供您想要的。
它可以写得更好,但应该让你知道你要写什么:
You can't add anything to the string directly to make the cell have bold characters.
Once you've written the string out to the cell, you'll need to go back and reprocess the cell.
For example:
This snippet will set only a portion of the cell to bold.
EDIT:
This code could be used to implement the above and give you what you want.
It could be written better, but should give you an idea of what you've got to write: