見出し画像

表の偶数行だけを削除するChatGPTと一緒に作ったやつ

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

このVBAプロシージャは、アクティブなExcelシートで、表の偶数行をすべて削除する仕組みを実現しています。コードの流れを分かりやすく解説します。


プロシージャの仕組み

1. アクティブシートを設定

まず、現在選択中のシート(アクティブシート)を対象とします。このシートのデータ範囲を操作します。

2. 最終行と最終列の取得

  • 最終行: データが入力されている一番下の行番号を調べます。

  • 最終列: データが入力されている一番右の列番号を調べます。

これにより、データがどの範囲に存在しているかを把握します。

3. 表データを配列に格納

Excelのデータを一時的に「配列」という形式に取り込みます。この配列を使うと、データの処理が高速で効率的になります。

4. 奇数行だけを抽出

  • 配列内を1行ずつ確認し、行番号が奇数の行だけを別の配列に移動します。

  • i Mod 2 <> 0 という条件で「行番号が偶数か奇数か」を判定しています。

5. 偶数行を削除して新しいデータを貼り付け

  • 元のデータ範囲(2行目以降)を一度削除。

  • 抽出した奇数行だけを再度貼り付けます。

これにより、偶数行がすべて取り除かれたデータが完成します。


主なポイント

  • データを配列に取り込むことで、行ごとの操作を簡単かつ効率的にしています。

  • 配列のサイズをあらかじめ計算し直し(ReDim)、不要な行を省いた新しい配列を作成しています。


こんな場面で役立つ

  • 大量のデータの中で、特定のパターン(奇数・偶数行)だけを取り除きたいとき。

  • Excelの基本機能だけでは手間がかかる操作を効率的に実現したいとき。


参考リンク

Sub 表の偶数行だけを削除するChatGPTと一緒に作ったやつ()
    Application.ScreenUpdating = False

    ' アクティブシートを設定
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Activate

    ' 最終行と最終列を取得
    Dim lastRow As Long, lastCol As Long
    lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row
    lastCol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column

    Dim table As Variant, table2 As Variant
    ' テーブル範囲を設定(見出し含めず)
    table = ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, lastCol)).Value

    ' 奇数行を抽出してtable2に格納
    Dim i As Long, j As Long, table2Row As Long
    table2Row = 0
    ReDim table2(1 To Int((UBound(table, 1) + 1) / 2), 1 To UBound(table, 2))

    For i = 1 To UBound(table, 1)
        If i Mod 2 <> 0 Then ' 奇数行を抽出
            table2Row = table2Row + 1
            For j = 1 To UBound(table, 2)
                table2(table2Row, j) = table(i, j)
            Next j
        End If
    Next i

    ' wsの2行目以降を削除
    ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, lastCol)).ClearContents

    ' wsの2行目からtable2を貼り付け
    ws.Range("A2").Resize(UBound(table2, 1), UBound(table2, 2)).Value = table2

    Application.ScreenUpdating = True
End Sub

関連するキーワード(ハッシュタグ)

#excel #できること #vba #偶数行削除 #奇数行抽出 #アクティブシート #最終行取得 #データ操作 #データ範囲 #配列操作 #効率化 #行削除 #表操作 #初心者向けVBA #コード解説 #自動化 #テーブル操作 #エクセル活用 #業務効率化



English Translation

Deleting Only Even Rows from a Table with ChatGPT

This explanation was created using ChatGPT.

This VBA procedure is designed to remove all even-numbered rows from a table in an active Excel sheet. Here's a simplified explanation of how the code works:


How the Procedure Works

1. Set the Active Sheet

The macro targets the currently active sheet. It identifies and processes the data range on this sheet.

2. Identify the Last Row and Column

  • Last Row: Finds the row number of the last non-empty cell.

  • Last Column: Identifies the column number of the last non-empty cell.

This step ensures the macro knows the boundaries of the data to be processed.

3. Store Table Data in an Array

The macro copies the table data into an array. This approach improves efficiency and simplifies row-wise processing.

4. Extract Odd Rows

  • Each row in the array is checked, and only rows with odd numbers are copied to a new array.

  • The condition i Mod 2 <> 0 determines whether a row number is even or odd.

5. Clear and Paste Updated Data

  • Deletes the original data range (starting from the second row).

  • Pastes the updated array containing only odd rows back into the sheet.

The result is a table with all even-numbered rows removed.


Key Features

  • Uses arrays for fast and efficient processing.

  • Dynamically resizes the array (ReDim) to store only the required rows.


When to Use

  • Managing large datasets where specific patterns (e.g., odd/even rows) need to be removed.

  • Automating repetitive tasks that would otherwise be tedious to perform manually in Excel.


References


Relevant Keywords (Hashtags)

#excel #capabilities #vba #deleteevenrows #extractoddrows #activesheet #lastrow #datahandling #dataprocessing #arrays #automation #deleteRows #tablemanipulation #vbaForBeginners #codeExplanation #workflow #tableOperations #excelEfficiency #businessAutomation

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