見出し画像

表の行を別シートにコピーするよ

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

このVBAプロシージャは、Excelのシートで選択している列のデータを使って、新しいシートを自動で作成し、そのシートにデータをコピーする仕組みになっています。具体的には、アクティブなシートの特定の列のセルにある値を新しいシート名にして、そのシートにデータをコピーします。

仕組みの解説

  1. `Application.ScreenUpdating = False`

    • 画面の更新を一時的に停止して、処理を高速化します。終わったら、最後に再度更新をオンにします。

  2. アクティブシートの設定

    • `Set ws = ActiveSheet` で、現在開いているシート(アクティブシート)を ws という変数に代入しています。

  3. 対象となる列の確認

    • `Col = ActiveCell.Column` で、アクティブなセルの列番号を取得します。ここで取得した列が、シート名に使用される値が入っている列になります。

  4. データの最終行を取得

    • `Rec = Cells(Rows.Count, Col).End(xlUp).Row` によって、選択した列のデータがどこまであるか、最後の行を取得しています。

  5. ループ処理でシートを作成

    • `For i = 2 To Rec` で、2行目から最終行までループしながら、各行のデータを使って新しいシートを作ります。

  6. シート名の設定

    • `sheetName = ws.Cells(i, Col).Value` により、各行の指定した列の値を取得し、それを新しいシートの名前にします。

  7. 新しいシートの作成

    • `Set newSheet = Sheets.Add(Before:=ws)` で、アクティブシートの左側に新しいシートを作成し、`newSheet.Name = sheetName` でシート名を設定します。

  8. データのコピー

    • `ws.Rows(1).Copy Destination:=newSheet.Cells(1, 1)` で元のシートの1行目(見出し)を新しいシートの1行目にコピーします。

    • `ws.Rows(i).Copy Destination:=newSheet.Cells(2, 1)` で、その行のデータを新しいシートの2行目にコピーします。

  9. `Application.ScreenUpdating = True`

    • 処理が終わった後、画面の更新を再度有効にします。

このプロシージャは、複数のデータを個別のシートに整理するのに便利です。例えば、ある列に商品名や顧客名が書かれていると、そのデータを使ってシートを分けることができます。


Sub 表の行を別シートにコピーするよ()
    Application.ScreenUpdating = False
    Dim sheetName As String
    Dim ws, newSheet As Worksheet
    Dim Col, Rec, i As Long
    
    ' アクティブシートを設定
    Set ws = ActiveSheet
    Col = ActiveCell.Column 'いまいる列
    Rec = Cells(Rows.count, Col).End(xlUp).row

    For i = 2 To Rec
        '基準列のセルにある値をシート名にする
        sheetName = ws.Cells(i, Col).Value
        
        ' アクティブシートの左に新規シートを追加
        Set newSheet = Sheets.Add(Before:=ws)
        newSheet.Name = sheetName
        
        ' 新規シートの1列目に見出し、2列目にレコードをコピー
        ws.Rows(1).Copy Destination:=newSheet.Cells(1, 1)
        ws.Rows(i).Copy Destination:=newSheet.Cells(2, 1)
    Next i
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ

#excel #vba #できること #シート作成 #データ整理 #行のコピー #自動処理 #列の値取得 #データ分割 #アクティブシート #新規シート作成 #画面更新 #シート名設定 #Excelシート操作 #ループ処理 #顧客データ #商品名管理 #コピー機能 #Excel時短 #Excel操作


English Translation

Copy Rows from a Table to a New Sheet

This explanation is created with ChatGPT.

This VBA procedure automatically creates a new sheet in Excel based on the data from a selected column in the active sheet, using the values in the cells as sheet names and copying corresponding data into these new sheets.

Breakdown of the Mechanism

  1. `Application.ScreenUpdating = False`

    • Temporarily stops screen updating to speed up the process. Screen updating will be enabled again at the end.

  2. Setting the Active Sheet

    • `Set ws = ActiveSheet` assigns the currently active sheet to the ws variable.

  3. Identify the Target Column

    • `Col = ActiveCell.Column` identifies the column number of the currently selected cell. This column will contain the values to be used as the names for new sheets.

  4. Get the Last Row of Data

    • `Rec = Cells(Rows.Count, Col).End(xlUp).Row` identifies the last row of data in the selected column.

  5. Loop to Create New Sheets

    • `For i = 2 To Rec` loops from the 2nd row to the last row to create a new sheet for each row.

  6. Set Sheet Name

    • `sheetName = ws.Cells(i, Col).Value` retrieves the value from the specified column in each row and uses it as the name for the new sheet.

  7. Create a New Sheet

    • `Set newSheet = Sheets.Add(Before:=ws)` creates a new sheet to the left of the active sheet, and `newSheet.Name = sheetName` sets the name of this new sheet.

  8. Copy Data

    • `ws.Rows(1).Copy Destination:=newSheet.Cells(1, 1)` copies the first row (header) from the active sheet to the new sheet.

    • `ws.Rows(i).Copy Destination:=newSheet.Cells(2, 1)` copies the data from the row in the active sheet to the second row of the new sheet.

  9. `Application.ScreenUpdating = True`

    • Screen updating is enabled again after the procedure completes.

This procedure is useful for organizing data into separate sheets. For example, if you have a list of products or customers in a column, you can automatically separate the data into individual sheets based on the values in that column.



Hashtags

#excel #vba #automation #sheetcreation #datamanagement #copyrows #automatedtask #getcolumnvalues #dataseparation #activesheet #newsheetcreation #screenupdate #sheetnamingsetup #exceloperations #loopingprocess #customerdata #productmanagement #copyfunction #timesavinginexcel #excelhandling

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