見出し画像

選択している列にデータが何件あるか集計してクリップボードに保存するよ

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

このプログラムは、ExcelのVBA(Visual Basic for Applications)を使って、選択中の列にあるデータを会社名ごとに集計し、その結果をクリップボードに保存します。結果は、クリップボードにコピーされるので、他のアプリケーションに簡単に貼り付けることができます。


プログラムの動き

  1. 画面更新を一時的にオフにする
    `Application.ScreenUpdating = False` で、プログラムが動いている間に画面のチラつきを防ぎます。

  2. 選択している列のデータを取得

    • `colNum` に選択した列の番号を格納します。

    • `c` には、選択した列でデータが入っている最後の行の番号を取得します。

    • `kaisya` に、その列の2行目から最終行までのデータを配列として取り込み、行列を入れ替えます。

  3. 会社名ごとに件数を集計
    `Scripting.Dictionary` というオブジェクトを使って、会社名をキーにして、それぞれの件数をカウントします。

    • もし既に存在する会社名なら、その件数を1増やします。

    • 存在しない場合は、新しくキーを作り、件数を1に設定します。

  4. 結果をまとめてタブ区切りの文字列にする
    各会社名とその件数をタブ区切り(スペースのような役割を果たす)で並べ、次の行に移るようにして `result` にまとめます。

  5. クリップボードにコピー
    作成した結果をクリップボードにコピーします。これにより、メモ帳やWordなどに貼り付けることができます。

  6. 完了メッセージを表示
    `MsgBox` を使って、「結果をクリップボードにコピーしました」というメッセージを表示し、ユーザーに処理が完了したことを知らせます。

  7. 画面更新を再開
    `Application.ScreenUpdating = True` で、画面更新を再び有効にします。


この記事のYouTube動画はこちら

この記事のYouTube動画はこちら

Sub 選択している列にデータが何件あるか集計してクリップボードに保存するよ()

    Application.ScreenUpdating = False
    Dim kaisya As Variant
    Dim colNum, c As Long
    '選択している列のデータを格納する
    colNum = Selection.Column
    c = ActiveSheet.Cells(Rows.count, colNum).End(xlUp).row
    kaisya = Cells(2, colNum).Resize(c, 1).Value
    kaisya = WorksheetFunction.Transpose(kaisya)
    
    '会社名ごとに件数を集計
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long
    For i = LBound(kaisya) To UBound(kaisya)
        If kaisya(i) <> "" Then
            If dict.exists(kaisya(i)) Then
                dict(kaisya(i)) = dict(kaisya(i)) + 1
            Else
                dict.Add kaisya(i), 1
            End If
        End If
    Next i
    
    '結果をタブ区切りでまとめる
    Dim result As String
    Dim key As Variant
    result = ""
    For Each key In dict.Keys
        result = result & key & vbTab & dict(key) & vbCrLf
    Next key

    '結果をクリップボードにコピー
    On Error Resume Next
    With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        .SetText result
        .PutInClipboard
    End With
    
    MsgBox "結果をクリップボードにコピーしました。", vbInformation
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ

#excel #できること #vba #クリップボード #データ集計 #タブ区切り #会社名 #選択列 #データ処理 #セル操作 #辞書オブジェクト #VisualBasic #コード解説 #行列変換 #データ分析 #オートメーション #データ管理 #プログラミング初心者 #自動化 #マクロ


英語での説明

Count the Data in the Selected Column and Save to Clipboard

This explanation is created using ChatGPT.

This program uses Excel VBA (Visual Basic for Applications) to count the data in the currently selected column by company name and save the result to the clipboard. The result can be easily pasted into other applications since it is stored in the clipboard.


How the Program Works

  1. Temporarily Disables Screen Updating
    `Application.ScreenUpdating = False` is used to prevent screen flickering while the program is running.

  2. Retrieves Data from the Selected Column

    • `colNum` stores the column number of the selected range.

    • `c` retrieves the row number of the last cell with data in the selected column.

    • `kaisya` stores the data from the 2nd row to the last row of that column as an array and transposes it.

  3. Counts Data by Company Name
    A `Scripting.Dictionary` object is used to count the occurrences of each company name.

    • If the company name already exists, the count is increased by one.

    • If it doesn't exist, a new key is added with a count of one.

  4. Compiles Results into a Tab-Delimited String
    It organizes each company name and its count into `result` with tab delimiters (which act like spaces) and moves to the next line for each.

  5. Copies to Clipboard
    The compiled result is copied to the clipboard, allowing it to be pasted into applications like Notepad or Word.

  6. Displays a Completion Message
    It uses `MsgBox` to show a message saying "The result has been copied to the clipboard," letting the user know that the process is complete.

  7. Re-enables Screen Updating
    `Application.ScreenUpdating = True` is used to enable screen updating again.


この記事のYouTube動画はこちら

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