串联公式中的粗体文本 [英] Bold text in a Concatenate formula

查看:101
本文介绍了串联公式中的粗体文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用串联公式时,我希望能够在句子中将一个或两个特定的单词加粗.下面显示了一个示例.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆