簡體   English   中英

VBA代碼將文本框中所選內容中的所有數字加粗?

[英]VBA code to bold all numbers within a selection in a textbox?

我有代碼將所選文本的字體更改為Arial且大小為10,這很簡單:

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

如何在此宏中添加加粗顯示此選擇項中的任何數字和破折號(“-”)?

來了...

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

另取:

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

將形狀設置為變量有助於獲取Intellisense。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM