使用VBA粗体显示文字

时间:2018-12-21 00:04:27

标签: excel vba excel-formula

这是我正在使用的单元格公式:

="Charge: "&(UPPER(AG343))&"    "&" Charging Staff: "&(AH343)&"    "&" Charging Staff: "&(AI343)&"    "&" CAP_ID: "&(AJ343)

VBA中是否有一种方法可以识别某个值正从对Cell "A"的引用中提取到Cell "B"中,并在{{ 1}}粗体?

我看过有关Excel功能限制文本操作的帖子。我不明白为什么我可以在单元格引用的结果或单元格公式中的一般文本上使用Cell "B"函数,但不允许在公式中使用粗体文本。

VBA中是否可以将粗体属性应用于单元格中的文本?

有没有一种方法可以对单元格引用使用Cell "A"属性来获取加粗文本?

1 个答案:

答案 0 :(得分:1)

这对于公式而言是不可能的,但对于vba来说是不可能的。

Sub test()
    Dim rng As Range
    Dim s As String
    Dim s1 As String, s2 As String, s3 As String
    Dim k1 As Integer, k2 As Integer, k3 As Integer
    Set rng = ActiveCell

    s1 = Range("AH343")
    s2 = Range("AI343")
    s3 = Range("Aj343")

    s = "Charging Staff: " & s1 & "    " & " Charging Staff: " & s2 & "    " & " CAP_ID: " & s3

    k1 = InStr(s, s1)
    k2 = InStr(s, s2)
    k3 = InStr(s, s3)
    rng = s
    With rng
        .Font.Bold = False
        .Characters(k1, Len(s1)).Font.Bold = True
        .Characters(k2, Len(s2)).Font.Bold = True
        .Characters(k3, Len(s3)).Font.Bold = True
    End With
End Sub

这与使用for语句的代码相同。

Sub test2()
    Dim rng As Range
    Dim s As String
    Dim vS As Variant, vK()
    Dim i As Integer
    Set rng = ActiveCell

    vS = Range("Ah343").Resize(1, 3)

    s = "Charging Staff: " & vS(1, 1) & "    " & " Charging Staff: " & vS(1, 2) & "    " & " CAP_ID: " & vS(1, 3)

    For i = 1 To UBound(vS, 2)
        ReDim Preserve vK(1 To i)
        vK(i) = InStr(s, vS(1, i))
    Next i
    rng = s
    With rng
        .Font.Bold = False
        For i = 1 To 3
            .Characters(vK(i), Len(vS(1, i))).Font.Bold = True
        Next i
    End With
End Sub