VBA代码加粗文本框中选择内的所有数字?

时间:2015-02-12 20:40:15

标签: excel excel-vba vba

我有代码将所选文本的字体更改为Arial和10号,这很简单:

Sub Arial10()
    With Selection.ShapeRange.TextFrame2.TextRange.Font
        .name = "Arial"
        .Size = 10
    End With
End Sub

如何在此选择中添加此宏以加粗任何数字和短划线(“ - ”)?

2 个答案:

答案 0 :(得分:2)

这就是......

Sub Arial10()
With Selection.ShapeRange.TextFrame2.TextRange.Font
    .Name = "Arial"
    .Size = 10


End With

'-----BOLD all numbers and dashes-----
With Selection.ShapeRange.TextFrame2.TextRange
    Dim i As Long
    For i = 1 To Len(.Text)
        If Mid(.Text, i, 1) Like "#" Or _
            Mid(.Text, i, 1) = "-" Then
                .Characters(i, 1).Font.Bold = True
        End If
    Next
End With

End Sub

答案 1 :(得分:2)

另一种观点:

Sub Arial10andBoldStuff()
Dim shp As ShapeRange
Dim i As Long
Dim Char As Object

Set shp = Selection.ShapeRange
With shp.TextFrame2.TextRange
    With .Font
        .Name = "Arial"
        .Size = 10
    End With
    For i = 1 To .Characters.Count
        Set Char = .Characters(i, 1)
        If IsNumeric(Char) Or Char = "-" Then
            Char.Font.Bold = True
        End If
    Next i
End With
End Sub

将形状设置为变量有助于获取智能感知。