見出し画像

セルにあるカンマ区切りの値を隣の列に出力するよ

この説明は、ChatGPTで作成しています。


このマクロは、カンマで区切られたデータが書かれているセルを選んで実行すると、
そのカンマごとにデータを分けて、右側のセルに自動で並べてくれるプログラムです。


たとえばこんなときに使います!

「A1」のセルに
りんご,バナナ,みかん
と書かれていたら、マクロを動かすと

  • A1 → りんご

  • B1 → バナナ

  • C1 → みかん

というふうに、自動で分けて並べてくれます。


どうやって動いているの?

やっていることは、とてもシンプルです。

  1. 選んだセルを1つずつチェックします

  2. カンマで区切って中身を分けます(「Split」機能を使っています)

  3. 分けた言葉を、元のセルの右側に順番に書いていきます


パソコン初心者でも安心ポイント

  • 分ける記号(今回はカンマ)は、必要に応じて変更できます
    たとえば、Split(txt, ",") の "," を "/" にすれば、スラッシュ区切りもOK!

  • マウスでセルを選ぶだけで使えるので、初心者でもカンタンに試せます


応用できることは?

  • 住所を「都道府県、市区町村、番地」に分けたいとき

  • 名前を「姓、名」に分けたいとき

  • CSVデータを読み込んで整えたいとき

など、実務でもよく使えるマクロです!


関連リンク

Sub セルにあるカンマ区切りの値を隣の列に出力するよ()
    Application.ScreenUpdating = False

    Dim myRng As Range
    Dim txt As String
    Dim shp As Shape
    Dim ws As Worksheet
    Set ws = ActiveSheet

    For Each myRng In Selection
        txt = myRng.Value
       
        Dim arr As Variant
        arr = Split(txt, ",") '区切り記号はここを変更する

        Dim i As Long
        For i = LBound(arr) To UBound(arr)
            myRng.Offset(0, i).Value = Trim(arr(i))
        Next i

    Next myRng

    Application.ScreenUpdating = True
End Sub

関連ハッシュタグ(キーワード20個)

#excel #できること #vba #セル分割 #カンマ区切り #データ整形 #業務効率化 #初心者向け #自動化 #split関数 #trim関数 #選択範囲 #ループ処理 #offset #文字列操作 #エクセル活用 #表作成 #vba入門 #ワークシート操作 #データ処理


English Translation


Put Comma-Separated Values from a Cell into the Next Columns

This explanation was created using ChatGPT.


This macro is useful when you have data in a cell that is separated by commas.
When you select those cells and run the macro, it automatically splits the text and places each part into the cells to the right.


Example Use Case

If cell A1 contains:
Apple,Banana,Orange
After running the macro:

  • A1 → Apple

  • B1 → Banana

  • C1 → Orange

It automatically splits and distributes the text!


How It Works

  1. Loops through each selected cell

  2. Uses the Split function to divide the content by commas

  3. Places each item in the cells to the right of the original


Beginner-Friendly Points

  • You can change the separator if needed
    For example, Split(txt, ",") can be changed to Split(txt, "/") for slashes

  • Just select cells with your mouse, no need for deep programming knowledge


When to Use

  • Splitting addresses into parts (e.g., prefecture, city, street)

  • Separating names into first and last names

  • Organizing imported CSV data

It’s a very practical and commonly used macro in real work!


Related Links


Related Hashtags (20 keywords)

#excel #できること #vba #セル分割 #カンマ区切り #データ整形 #業務効率化 #初心者向け #自動化 #split関数 #trim関数 #選択範囲 #ループ処理 #offset #文字列操作 #エクセル活用 #表作成 #vba入門 #ワークシート操作 #データ処理

いいなと思ったら応援しよう!