見出し画像

表の列を奇数と偶数列にわけて別のシートにコピーするChatGPTと一緒につくったやつ

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

このVBAプロシージャは、アクティブなシートにある表の列を「奇数列」と「偶数列」に分けて、それぞれ新しいシートにコピーする仕組みです。

手順と動作の概要

  1. スクリーン更新の停止
    最初に `Application.ScreenUpdating = False` として、コード実行中の画面の更新を一時停止しています。これにより処理が速くなります。

  2. 変数の準備
    次に、シートや列番号を管理するための変数を準備しています。たとえば、`ws` は元のシートを、`kis` と `gus` は奇数列と偶数列のコピー先シートを指します。

  3. コピー先のシート作成
    「奇数列」と「偶数列」のシートを新しく作成し、それぞれ `kis` と `gus` という変数にセットしています。

  4. 列の数を取得
    アクティブシート(元のシート)の列数を調べるため、`allColumn` という変数を使っています。これにより、表の最後の列が何列目まであるかがわかります。

  5. 奇数・偶数列のコピー処理
    次に、`For i = 1 To allColumn` で、1列目から最後の列まで1列ずつ順番に進みます。

    • 奇数列であれば「奇数列シート(`kis`)」にコピーされ、`kisColumn` が1つ増えます。

    • 偶数列であれば「偶数列シート(`gus`)」にコピーされ、`gusColumn` が1つ増えます。

  6. スクリーン更新の再開
    最後に `Application.ScreenUpdating = True` で、スクリーン更新を再開します。

このプロシージャを実行することで、元のシートの奇数列と偶数列が別々のシートに分けられ、見やすく整理されます。たとえば、1列目、3列目、5列目のデータが「奇数列シート」にコピーされ、2列目、4列目、6列目のデータが「偶数列シート」にコピーされる形です。


Sub 表の列を奇数と偶数列にわけて別のシートにコピーするChatGPTと一緒につくったやつ()
    Application.ScreenUpdating = False
    Dim ws, kis, gus As Worksheet
    Dim allColumn, kisColumn, gusColumn, i As Long
    
    ' アクティブシートを設定
    Set ws = ActiveSheet
    
    ' 奇数列シートと偶数列シートを作成
    Set kis = Sheets.Add(before:=ws)
    kis.Name = "奇数列"
    Set gus = Sheets.Add(before:=kis)
    gus.Name = "偶数列"
    
    ' アクティブシートのカラム数を取得
    allColumn = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
    
    ' 初期化: コピー先の列番号を1に設定
    kisColumn = 1
    gusColumn = 1

    ' 各カラムを奇数・偶数に分けてコピー
    For i = 1 To allColumn
        If i Mod 2 = 1 Then
            ws.Columns(i).Copy Destination:=kis.Cells(1, kisColumn)
            kisColumn = kisColumn + 1
        Else
            ws.Columns(i).Copy Destination:=gus.Cells(1, gusColumn)
            gusColumn = gusColumn + 1
        End If
    Next i

    Application.ScreenUpdating = True
End Sub

キーワード

#excel #できること #vba #奇数列 #偶数列 #シート作成 #列コピー #表データ分割 #アクティブシート #Forループ #If文 #自動化 #列数取得 #VBA解説 #シート追加 #列操作 #セル操作 #データ整理 #プログラミング初心者 #業務効率化


Explanation in English

Copy Odd and Even Columns Separately to New Sheets

This explanation is created by ChatGPT.

This VBA procedure divides the columns of a table on an active sheet into “odd” and “even” columns, and then copies each group to new sheets.

Steps and Functions

  1. Stop Screen Updating
    `Application.ScreenUpdating = False` is used to temporarily stop screen updates during execution, speeding up processing.

  2. Prepare Variables
    Variables are prepared to manage sheets and column numbers, where `ws` represents the original sheet, and `kis` and `gus` represent the target sheets for odd and even columns.

  3. Create Destination Sheets
    New sheets called “Odd Columns” and “Even Columns” are created for copying each respective column set.

  4. Get Total Column Count
    The number of columns on the active sheet is calculated using `allColumn`, helping identify the last populated column.

  5. Copy Odd/Even Columns
    The loop `For i = 1 To allColumn` cycles through each column.

    • Odd-numbered columns are copied to the “Odd Columns” sheet (`kis`), incrementing `kisColumn`.

    • Even-numbered columns are copied to the “Even Columns” sheet (`gus`), incrementing `gusColumn`.

  6. Resume Screen Updating
    Finally, `Application.ScreenUpdating = True` resumes screen updates.

When you run this procedure, odd-numbered and even-numbered columns are separated into two sheets for easier viewing. For example, columns 1, 3, and 5 are copied to the “Odd Columns” sheet, while columns 2, 4, and 6 go to the “Even Columns” sheet.


Keywords

#excel #functions #vba #oddcolumns #evencolumns #sheetcreation #columncopy #tabularsplit #activesheet #loop #ifstatement #automation #columncount #vbaguide #addsheet #columnhandling #cellmanipulation #datacleanup #beginners #workflow

この記事が気に入ったらサポートをしてみませんか?