'本マクロは、チェックしたいセルを選択後に実行してください。 Sub 変換1_1() Dim r As Integer, c As Integer r = ActiveCell.Row c = ActiveCell.Column MsgBox (Chr(64 + c) & r & "セルのデータ型→" & VarType(Cells(r, c))) End Sub |
Sub 変換2_1() For i = 1 To 3 With Cells(i, 1) shu = .Value .NumberFormat = "@" .Value = shu End With Next End Sub --------------------------------------------------------- Sub 変換2_2() For i = 1 To 3 With Cells(i, 1) shu = .Value .NumberFormat = "@" .Value = CStr(shu) End With Next End Sub --------------------------------------------------------- Sub 変換2_3() For i = 1 To 3 With Cells(i, 1) .NumberFormat = "@" .Value = CStr(.Value) End With Next End Sub |
No | 項目 | VBA | 文字変数 | 例 | 結果 | 記事 |
1 | 文字数取得 | Len | m1="ABCDEFGH" |
t1=Len(m1) | t1→8 | 関数 |
2 | 文字列の左より取り出し | Left | t2=Left(m1, 4) | t2→ABCD | 関数 | |
3 | 文字列の右より取り出し | Right | t3=Right(m1, 4) | t3→EFGH | 関数 | |
4 | 文字列の指定個所取り出し |
Mid |
t4=Mid(m1, 3,2) | t4→CD | 関数 | |
5 | t5=Mid(m1, 5) | t5→EFGH | ||||
6 | 指定した文字の位置取得 | InStr | t6=InStr(3, m1, "e", 1) | t6→5 | 関数 | |
6a | 指定文字を後ろから位置取得 | InStrRev | t6a = InStrRev(m1, "E") | t6a→5 | 関数 | |
7 | 数字→文字 | Str | t7=Str(999) & 1234 | t7→9991234 | 関数 | |
8 | 文字→数字 IsNumeric(文字)で可はTrue |
Val |
m2="1111" m3="123Km" m4="w123Km" |
t8=Val(m2) + 1234 | t8→2345 | 関数 |
9 | t9=Val(m3) | t9→1357 | ||||
10 | t10=Val(m4) | t10→0 | ||||
11 | アルファベットを小文字に | LCase | m5="ABCD" m6="abcd" m7=" abcd" m8="abcd " |
t11=LCase(m5) | t11→abcd | 関数 |
12 | アルファベットを大文字に | UCase | t12=UCase(m6) | t12→ABCD | 関数 | |
13 | 文字列の比較(パターン認識) |
Like |
t13= m5 Like m5 | t13→True | 演算子 | |
14 | t14= m5 Like "*B*" | t14→True | 演算子 | |||
15 | t15= m5 Like "B" | t15→False | 演算子 | |||
16 | 文字列の比較 | StrComp | t16=StrComp(m5, m6, 1) | t16→0 | 関数 | |
17 | 先頭の空白削除 | LTrim | t17 = LTrim(m7) | t17→4文字 | 関数 | |
17a | 最後尾の空白削除 | RTrim | t17a =RTrim(m8) | t17a→4文字 | ||
18 | 大文字に変換 | StrConv |
m1="abcd" m2="DEFG" m3="deアイ" m4="ABウエ" m5="あいうえ" m6="カキクケ" m7 = "あい" m8 = "カキ" |
t18 = StrConv(,m1, 1) | t18→ABCD | 関数 |
19 | 小文字に変換 | t19 = StrConv(m2, 2) | t19→defg | |||
20 | 先頭を大文字に変換 | t20 = StrConv(m1, 3) | t20→Abcd | |||
21 | 半角を全角に変換 | t21 = StrConv(m3, 4) | t21→deアイ | |||
22 | 全角を半角に変換 | t22 = StrConv(m4, 8) | t22→ABウエ | |||
23 | 全角ひらがなをカタへ | t23 = StrConv(m5, 16) | t23→アイウエ | |||
24 | 全角カナをひらがなへ | t24 = StrConv(m6, 32) | t24→かきくけ | |||
18a | 大文字に変換 | t18a = StrConv(m1, vbUpperCase) | t18a→ABCD | |||
19a | 小文字に変換 | t19a= StrConv(m2,vbLowerCase) | t19a→defg | |||
20a | 先頭を大文字に変換 | t20a = StrConv(m1, vbProperCase) | t20a→Abcd | |||
21a | 半角を全角に変換 | t21a = StrConv(m3, vbWide) | t21a→deアイ | |||
22a | 全角を半角に変換 | t22a = StrConv(m4, vbNarrow) | t22a→ABウエ | |||
23a | 全角ひらがなをカタへ | t23a = StrConv(m5, vbKatakana) | t23a→アイウエ | |||
24a | 全角カナをひらがなへ | t24a = StrConv(m6, vbHiragana) | t24→かきくけ | |||
25 | 文字の繰返し |
Space | t25 = m7 & Space(2) & m8 | t25→あい カキ | 関数 | |
26 | String | t26 = String(3, "○") | t26→○○○ | 関数 | ||
27 | 文字列変数に左詰で記憶 | LSet | LSet mystr$ = m5:Cells(1, 2) = mystr$ | ステー トメント | ||
28 | 文字列変数に右詰で記憶 | RSet | RSet mystr$ = m5: Cells(1, 2) = mystr$ |
Dim m1 As String, m2 As String Dim m3 As String, m4 As String Dim m5 As String, m6 As String -------------------------------------- Sub Macro1() m1 = "ABCDEFGH" t1 = Len(m1) MsgBox "t1→ " & t1 End Sub -------------------------- Sub Macro2() m1 = "ABCDEFGH" t2 = Left(m1, 4) MsgBox "t2→ " & t2 End Sub -------------------------- Sub Macro3() m1 = "ABCDEFGH" t3 = Right(m1, 4) MsgBox "t3→ " & t3 End Sub -------------------------- Sub Macro4() m1 = "ABCDEFGH" t4 = Mid(m1, 3, 2) MsgBox "t4→ " & t4 End Sub -------------------------- Sub Macro5() m1 = "ABCDEFGH" t5 = Mid(m1, 5) MsgBox "t5→ " & t5 End Sub -------------------------- Sub Macro6() m1 = "ABCDEFGH" t6 = InStr(3, m1, "e", 1) MsgBox "t6→ " & t6 End Sub -------------------------- Sub Macro6a() m1 = "ABCDEFGH" t6a = InStrRev(m1, "E") MsgBox "t6a→ " & t6a End Sub -------------------------- Sub Macro7() t7 = Str(999) & 1234 MsgBox "t7→ " & t7 End Sub -------------------------- Sub Macro8() m2 = "1111" t8 = Val(m2) + 1234 MsgBox "t8→ " & t8 End Sub -------------------------- Sub Macro9() m3 = "123Km" t9 = Val(m3) + 1234 MsgBox "t9→ " & t9 End Sub Sub Macro10() m4 = "w123Km" t10 = Val(m4) MsgBox "t10→ " & t10 End Sub -------------------------- Sub Macro11() m5 = "ABCD" t11 = LCase(m5) MsgBox "t11→ " & t11 End Sub -------------------------- Sub Macro12() m6 = "abcd" t12 = UCase(m6) MsgBox "t12→ " & t12 End Sub -------------------------- Sub Macro13() m5 = "ABCD" t13 = m5 Like m5 MsgBox "t13→ " & t13 End Sub -------------------------- Sub Macro14() m5 = "ABCD" t14 = m5 Like "*B*" MsgBox "t14→ " & t14 End Sub -------------------------- Sub Macro15() m5 = "ABCD" t15 = m5 Like "B" MsgBox "t15→ " & t15 End Sub -------------------------- Sub Macro16() m5 = "ABCD": m6 = "abcd" t16 = StrComp(m5, m6, 1) MsgBox "t16→ " & t16 End Sub -------------------------- Sub Macro17() m7 = " abcd" t17a = m7 t17b = LTrim(m7) MsgBox "t17a→" & Len(t17a) & "字 " & "t17b→" & Len(t17b) & "字" End Sub -------------------------- Sub Macro17a() m8 = "abcd " t17a = m8 t17b = RTrim(m8) MsgBox "t17a→" & Len(t17a) & "字 " & "t17b→" & Len(t17b) & "字" End Sub -------------------------- Sub Macro18() m1 = "abcd" t18 = StrConv(m1, 1) MsgBox "t18→ " & t18 End Sub -------------------------- Sub Macro19() m2 = "DEFG" t19 = StrConv(m2, 2) MsgBox "t19→ " & t19 End Sub -------------------------- Sub Macro20() m1 = "abcd" t20 = StrConv(m1, 3) MsgBox "t20→ " & t20 End Sub -------------------------- Sub Macro21() m3 = "deアイ" t21 = StrConv(m3, 4) MsgBox "t21→ " & t21 End Sub -------------------------- Sub Macro22() m4 = "ABウエ" t22 = StrConv(m4, 8) MsgBox "t22→ " & t22 End Sub -------------------------- Sub Macro23() m5 = "あいうえ" t23 = StrConv(m5, 16) MsgBox "t23→ " & t23 End Sub -------------------------- Sub Macro24() m6 = "カキクケ" t24 = StrConv(m6, 32) MsgBox "t24→ " & t24 End Sub Sub Macro25() m7 = "あい" m8 = "カキ" t25 = m7 & Space(2) & m8 MsgBox "t25→ " & t25 End Sub -------------------------- Sub Macro26() m5 = "あいうえ" t26 = String(3, "○") MsgBox "t26→ " & t26 End Sub Sub Macro27() m5 = "あいうえ" mystr$ = String(15, "*") LSet mystr$ = m5 Cells(1, 2) = mystr$ End Sub -------------------------- Sub Macro28() m5 = "あいうえ" mystr$ = String(15, "*") RSet mystr$ = m5 Cells(1, 2) = mystr$ End Sub |
No | 項目 | VBA | 変数 | 例 | 結果 | 記事 |
29a |
文字列の場所 を指定し変更 |
Characters |
注1 |
myrange.Characters(4, 3).Font.ColorIndex = 3 ※ 4:Start(省略は先頭から)、3:文字数(省略は以降全部) |
DEFが赤色 |
プロ パティ |
29b | myrange.Characters(3, 2).Font .FontStyle = "太字" | CD太字 | ||||
29c | myrange.Characters(4, 3).Insert "aaa" | ABCaaaG | ||||
30a | 文字列の一部 を置換え |
Replace |
注1 |
myrange.Replace "BCD", "abc123" | Aabc123EFG | メ ソッド |
30b | myrange.Replace What:="BCD", Replacement:="abc123", _ LookAt:=xlPart, SearchOrder:=xlByRows, _ MatchCase:=False, MatchByte:=False ※MatchCase:=False(大文字小文字区別しない)(True区別) ※MatchByte:=False(半角全角区別しない)(True区別する) |
Aabc123EFG |
||||
30c | myrange.Replace "*D", "" | EFG, | ||||
30d | myrange.Replace "D*", "" | ABC | ||||
31 |
ワークシート 関数で置換え |
Substitute |
|
Cells(1, 1) = Application.WorksheetFunction. _ Substitute("?ABCD", "?", "abc123") ※"?ABCD"→文字列、"?"→検索文字列 "abc123"→置換文字列 ※ 文字列に「?*」があり、この「? *」を検索文字に 指定して置換えたい場合、前14項の方法では ワイルドカ−ド記号となってしまい変換できな いが、ワークシート関数ではできます。 |
abc123ABCD |
WS 関数 |
Dim myrange As Range Sub Macro29a() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Characters(4, 3).Font.ColorIndex = 3 End Sub ---------------------------------------------------------- Sub Macro29b() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Characters(3, 2).Font.FontStyle = "太字" End Sub ---------------------------------------------------------- Sub Macro29c() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Characters(4, 3).Insert "aaa" End Sub ---------------------------------------------------------- Sub Macro30a() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Replace "BCD", "abc123" End Sub ---------------------------------------------------------- Sub Macro30b() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Replace What:="BCD", Replacement:="abc123", _ LookAt:=xlPart, SearchOrder:=xlByRows, _ MatchCase:=False, MatchByte:=False End Sub ---------------------------------------------------------- Sub Macro30c() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Replace "*D", "" End Sub ---------------------------------------------------------- Sub Macro30d() Set myrange = Cells(1, 2) myrange.Value = "ABCDEFG" myrange.Replace "D*", "" End Sub ---------------------------------------------------------- Sub Macro31() Cells(1, 2) = Application.WorksheetFunction. _ Substitute("?ABCD", "?", "abc123") End Sub |