用VBA粗体显示文本 [英] Bolding Text with VBA
问题描述
这是我正在使用的单元格公式:
Here's the cell formula I'm using:
="Charge: "&(UPPER(AG343))&" "&" Charging Staff: "&(AH343)&" "&" Charging Staff: "&(AI343)&" "&" CAP_ID: "&(AJ343)
VBA中是否有一种方法可以识别某个值已从对 Cell"B"
的引用中提取到 Cell"A"
中,并进行了文本表示单元格"A"
粗体中的单元格"B"
中的引用是什么?
Is there a way in VBA to recognize that a value is being pulled into Cell "A"
from a reference to Cell "B"
and to make the text representation of the reference from Cell "B"
in Cell "A"
bold?
我看过有关Excel功能限制文本操作的帖子.我不明白为什么我可以在单元格引用的结果或单元格公式中的一般文本上使用 UPPER
函数,但不允许在公式中使用粗体文本.
I've seen posts on this subject on the limitations of Excel functions to manipulate text. I don't understand why I can use the UPPER
function on the results of a cell reference or on text in general in a cell formula, but not be allowed to bold text in a formula.
VBA中是否有一种方法可以将粗体属性应用于单元格中的文本?
Is there a way in VBA to apply the Bold property to text in a cell?
是否可以在单元格引用中使用"ActiveCell.Characters"
属性以获取加粗文本?
Is there a way to use the "ActiveCell.Characters"
property to the cell references to get bolded text?
推荐答案
对于公式,这是不可能的,但是对于vba,这是不可能的.
This is not possible with the formula, but with 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语句的代码相同.
This is the same code using the for statement.
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
这篇关于用VBA粗体显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!