连接公式中的粗体文本 [英] Bold text in a Concatenate formula
问题描述
在使用连接公式时,我希望能够将句子中的一两个特定单词加粗.示例如下所示.
I would like to be able to make one or two specific words bold in my sentence when using a concatenate formula. An example is shown below.
第一句话是使用连接公式.第二句话是手动输入和格式化的.有没有办法在连接公式中使用这种格式,而不必每次都手动进行?
The first sentence is using the concatenate formula. The second sentence is manually typed and formatted. Is there a way to have this formatting in the concatenate formula without having to do it manually every time?
请注意,这只是一个示例,我可能需要使用它在不同的句子中将三个连续单词的字符串加粗.如果提供了一个通用规则,我可以继续前进,那就太好了!
Please note that this is just an example and I may need to use it to make a string of three consecutive words bold in a different sentence. If a general rule was provided that I can work with going forwards that would be great!
我有点精通公式,但从未使用过 VBA.但是我怀疑这个问题的解决方案可能只能使用 VBA.如果需要 VBA 解决方案,请体谅,因为我需要一些时间和精力来启动和理解.
I am somewhat proficient with formulae but have never used VBA. However I suspect the solution for this problem may only be available using VBA. Please be considerate if a VBA solution is required as it'll take some time and effort for me to start-up and understand.
感谢您的时间和帮助.
Public Sub ExampleConcatenate()
Dim str1 As String, str2 As String, str3 As String, str4 As String, str5 As String, str6 As String
str1 = "First string "
str2 = "Second string "
str3 = "Third string"
str4 = "Fourth string "
str5 = "Fifth string "
str6 = "Sixth string"
Range("A1").Value = str1 & str2 & str3 & str4 & str5 & str6 'concatenate strings
'format bold starts 1 character after str1 and is as long as str2
Range("A1").Characters(Start:=Len(str1) + 1, Length:=Len(str2)).Font.Bold = True
End Sub
我将如何进一步扩展最后一部分以将第四和第六个字符串加粗?
How would I further extend the final part to make the fourth and sixth strings bold?
推荐答案
如果单元格文本中包含公式,则无法设置该单元格文本中单个字符的格式.
Excel 不支持.
You cannot format individual characters in a cell text if that cell contains a formula.
Excel doesn't support that.
唯一的解决方法是将该单元格文本写为常量文本(使用 VBA)而不是公式(如果满足您的要求).
The only workaround is to write that cell text as constant text (with VBA) instead of a formula (if that meets your requirement).
然后您可以使用以下格式设置单个字符的格式:
Then you can format individual characters with:
Range("A1").Characters(Start:=1, Length:=10).Font.Bold = True
所以要部分格式化字符串,您可以调整以下示例
So to partly format a string you could adjust the following example
Public Sub ExampleConcatenate()
Dim str1 As String, str2 As String, str3 As String
str1 = "First string "
str2 = "Second string "
str3 = "Third string"
Range("A1").Value = str1 & str2 & str3 'concatenate strings
'format bold starts 1 character after str1 and is as long as str2
Range("A1").Characters(Start:=Len(str1) + 1, Length:=Len(str2)).Font.Bold = True
End Sub
对于更多的子字符串,使用数组会更容易.
For more sub strings it would be easier to use an array.
Public Sub ExampleConcatenate()
Dim SubStrings As Variant
SubStrings = Array("First string ", _
"Second string ", _
"Third string ", _
"Fourth string ", _
"Fifth string ", _
"Sixth string")
Range("A1").Value = Join(SubStrings, "")
'Note array counting starts with 0 not 1 so "First string" is SubStrings(0)
'format bold starts 1 character after str1 and is as long as str2
Range("A1").Characters(Start:=Len(SubStrings(0)) + 1, Length:=Len(SubStrings(1))).Font.Bold = True
'format sub string 4
Range("A1").Characters(Start:=Len(SubStrings(0)) + Len(SubStrings(1)) + Len(SubStrings(2)) + 1, Length:=Len(SubStrings(3))).Font.Bold = True
'format sub string 6
Range("A1").Characters(Start:=Len(SubStrings(0)) + Len(SubStrings(1)) + Len(SubStrings(2)) + Len(SubStrings(3)) + Len(SubStrings(4)) + 1, Length:=Len(SubStrings(5))).Font.Bold = True
End Sub
这篇关于连接公式中的粗体文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!