![]() |
セル内の文字の一部だけ色やサイズを変える:Excel VBA入門 |
スポンサードリンク | |
Sub mojiiro() Dim myRng As Range Dim myStr As String Dim i As Integer '----元の書式に設定する For Each myRng In Selection With myRng.Font .ColorIndex = xlColorIndexAutomatic .Size = 11 End With ’----セルの文字列を1文字ずつ順番に調べる For i = 1 To Len(myRng) myStr = Mid(myRng.Value, i, 1) '----数字があったら書式を変更する If myStr Like "[0-9]" Then With myRng.Characters(Start:=i, Length:=1).Font .ColorIndex = 3 .Size = 14 End With End If Next i Next myRng End Sub |
Sub mojiiro2() Dim RE, c Dim myRng As Range Dim myStr As String Dim i As Integer Set RE = CreateObject("VBScript.RegExp") With RE .Pattern = "[0-9]" '----パターンを設定 .IgnoreCase = True .Global = False For Each myRng In Selection For i = 1 To Len(myRng) myStr = Mid(myRng.Value, i, 1) If .Test(myStr) Then With myRng.Characters(Start:=i, Length:=1).Font .ColorIndex = 3 .Size = 14 End With End If Next i Next myRng End With Set RE = Nothing End Sub |
Sub mojiiro3() Dim RE, c Dim myRng As Range Dim myStr As String Dim i As Integer Set RE = CreateObject("VBScript.RegExp") With RE .Pattern = "[0-9]+" .Global = False For Each myRng In Selection For Each c In .Execute(myRng.Value) With myRng.Characters(c.firstindex + 1, c.Length).Font .ColorIndex = 3 .Size = 14 End With Next c Next myRng End With Set RE = Nothing End Sub |
スポンサードリンク
PageViewCounter
Since2006/2/27