你如何在 VBA 中格式化文本/字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1405988/
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
How do you format text/strings in VBA?
提问by achinda99
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.
在下面的代码中,我使用一些输入参数,文本或单元格,并使用我需要的格式将它们组合成一个字符串。我需要将 Task_Name 加粗,以及像“Lead :”这样的文本。我知道你不能用可变粗体来制作文本,但是我该怎么做呢?我存储值的这个单元格最终用于 Word 邮件合并。
I need to format part of a string. In the code below, I need to make Task_Name, "Lead : ", etc. all bold.
我需要格式化字符串的一部分。在下面的代码中,我需要将 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
Also, I know it's not the cleanest code, so if there are any other suggestions for improving it, they are most welcome.
另外,我知道这不是最干净的代码,所以如果有任何其他改进它的建议,他们是最受欢迎的。
Thanks!
谢谢!
回答by Sk93
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:
将字符串写入单元格后,您需要返回并重新处理单元格。例如:
With ActiveCell.Characters(Start:=11, Length:=6).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
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:
此代码可用于实现上述内容并为您提供所需的内容。它可以写得更好,但应该让你知道你必须写什么:
Public Sub FormatOuput()
Dim i As Integer
'Format Task_name
i = InStr(1, ActiveCell.Text, vbLf)
MakeBold 1, i
'Format 'Lead'
MakeBold i + 1, 4
'Format 'Ambassadors'
i = InStr(i + 1, ActiveCell.Text, vbLf)
MakeBold i+1, 11
'Format 'Instructions'
i = InStr(i + 1, ActiveCell.Text, vbLf)
MakeBold i+1, 10
End Sub
Public Sub MakeBold(startPos As Integer, charCount As Integer)
With ActiveCell.Characters(start:=startPos, length:=charCount).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
End Sub

