Sub Sample1()
Dim buf As String, RightStr As String, i As Long
buf = Range("A1")
''変数bufを先頭から1文字ずつチェックする
''もし任意の数値だったら、そこから後ろを変数RightStrに格納する
''Val(RightStr)で数値部分を抜き出す
''Val(RightStr)を、Format関数で整形した書式に置き換える
End Sub
変数bufを(文字数分だけ)先頭から1文字ずつチェックするのだから・・・
Sub Sample1()
Dim buf As String, RightStr As String, i As Long
buf = Range("A1")
For i = 1 To Len(buf)
If Mid(buf, i, 1) が 任意の数値
''もし任意の数値だったら、そこから後ろを変数RightStrに格納する
End If
Next i
''Val(RightStr)で数値部分を抜き出す
''Val(RightStr)を、Format関数で整形した書式に置き換える
End Sub
任意の数値かどうかはLike演算子を使って・・・
Sub Sample1()
Dim buf As String, RightStr As String, i As Long
buf = Range("A1")
For i = 1 To Len(buf)
If Mid(buf, i, 1) Like "#" Then
RightStr = Mid(buf, i)
Exit For
End If
Next i
''Val(RightStr)で数値部分を抜き出す
''Val(RightStr)を、Format関数で整形した書式に置き換える
End Sub
数値の抜き出しと、整形と、置換は一気にできそうだな・・・
Sub Sample1()
Dim buf As String, RightStr As String, i As Long
buf = Range("A1")
For i = 1 To Len(buf)
If Mid(buf, i, 1) Like "#" Then
RightStr = Mid(buf, i)
Exit For
End If
Next i
Range("B1") = Replace(buf, Val(RightStr), Format(Val(RightStr), "#,###"))
End Sub
Sub Sample2()
Dim buf As String, RE, reMatch, reValue
Set RE = CreateObject("VBScript.RegExp")
buf = Range("A2")
With RE
.Pattern = 数値が1回以上連続するブロック
.Global = True ''←文字列内をすべて検索するオプション(この場合は必須)
Set reMatch = .Execute(buf)
''もし見つかったら
''見つかったすべての数値を取り出して
''Format関数で整形して
''Replace関数で置換する
End With
Set RE = Nothing
Range("B2") = buf
End Sub
Sub Sample2()
Dim buf As String, RE, reMatch, reValue
Set RE = CreateObject("VBScript.RegExp")
buf = Range("A2")
With RE
.Pattern = "\d+"
.Global = True
Set reMatch = .Execute(buf)
If reMatch.Count > 0 Then
''見つかったすべての数値を取り出して
''Format関数で整形して
''Replace関数で置換する
End If
End With
Set RE = Nothing
Range("B2") = buf
End Sub
Sub Sample2()
Dim buf As String, RE, reMatch, reValue
Set RE = CreateObject("VBScript.RegExp")
buf = Range("A2")
With RE
.Pattern = "\d+"
.Global = True
Set reMatch = .Execute(buf)
If reMatch.Count > 0 Then
For Each reValue In reMatch
''Format関数で整形して
''Replace関数で置換する
Next reValue
End If
End With
Set RE = Nothing
Range("B2") = buf
End Sub
整形と置換部分は、先の「1回しか登場しない場合」と同じですから・・・
Sub Sample2()
Dim buf As String, RE, reMatch, reValue
Set RE = CreateObject("VBScript.RegExp")
buf = Range("A2")
With RE
.Pattern = "\d+"
.Global = True
Set reMatch = .Execute(buf)
If reMatch.Count > 0 Then
For Each reValue In reMatch
buf = Replace(buf, reValue, Format(reValue, "#,###"))
Next reValue
End If
End With
Set RE = Nothing
Range("B2") = buf
End Sub