見出し画像

いまいる列にある文章の文字数を分析するマクロの例

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

このスクリプトは、ExcelのVBA(Visual Basic for Applications)を使って、選択した列の文章の文字数を分析するものです。

スクリプトの流れ

  1. 選択した列のデータを取得

    • 現在選択している列のデータをすべて取得し、配列に格納します。

  2. 各セルの文字数を計算

    • 取得したデータを1行ずつチェックし、それぞれの文字数を計算します。

  3. 統計データを算出

    • 最小値(最も短い文字数)

    • 最大値(最も長い文字数)

    • 平均値(すべての文字数の平均)

    • 最頻値(最も頻繁に出現する文字数)

  4. クリップボードにコピー

    • 計算結果をクリップボードにコピーします。

    • これにより、結果をすぐにExcelや他のアプリケーションに貼り付けることができます。

  5. メッセージボックスで通知

    • 「結果をクリップボードにコピーしました。」というメッセージが表示され、処理が完了します。

ポイント

  • 画面の更新を一時停止することで、処理速度を向上させています。

  • 辞書(Dictionary)を使って最頻値を計算しているため、効率的にデータを集計できます。

  • エラー処理(On Error Resume Next) を入れているので、クリップボードの処理でエラーが発生してもスクリプトが止まることはありません。

使い方

  1. Excelで分析したい列を選択します(1列だけ選択してください)。

  2. マクロを実行すると、選択した列の文字数が分析されます。

  3. 結果がクリップボードにコピーされるので、Excelやメモ帳に「Ctrl + V」で貼り付けて確認できます。

活用例

  • 顧客リストの名前の文字数を分析する

  • 商品説明の長さを統計的に調べる

  • アンケートの自由記述欄の文字数を確認する


関連リンク

Sub いまいる列にある文章の文字数を分析するよ2()

    Application.ScreenUpdating = False
    Dim txt As Variant
    Dim colNum, c As Long
    '選択している列のデータを格納する
    colNum = Selection.Column
    c = ActiveSheet.Cells(Rows.count, colNum).End(xlUp).Row
    txt = Cells(2, colNum).Resize(c - 1, 1).Value
    txt = WorksheetFunction.Transpose(txt)
    
    Dim i As Long
    Dim kazu() As Long
    ReDim kazu(LBound(txt) To UBound(txt))
    
    ' 文字数を取得
    For i = LBound(txt) To UBound(txt)
        kazu(i) = Len(txt(i))
    Next i
    
    ' 最小値、最大値、平均値を計算
    Dim minVal As Long, maxVal As Long, sumVal, ave As Long
    minVal = WorksheetFunction.min(kazu)
    maxVal = WorksheetFunction.max(kazu)
    sumVal = WorksheetFunction.Sum(kazu)
    ave = sumVal / (UBound(kazu) - LBound(kazu) + 1)
    
    ' 最頻値を計算
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    For i = LBound(kazu) To UBound(kazu)
        If dict.Exists(kazu(i)) Then
            dict(kazu(i)) = dict(kazu(i)) + 1
        Else
            dict.Add kazu(i), 1
        End If
    Next i
    
    Dim key As Variant, maxCount, hin As Long
    maxCount = 0
    For Each key In dict.Keys
        If dict(key) > maxCount Then
            maxCount = dict(key)
            hin = key
        End If
    Next key
    
    ' 結果をタブ区切りでまとめる
    Dim result As String
    result = ""
    result = result & "最小" & vbTab & minVal & vbCrLf
    result = result & "最大" & vbTab & maxVal & vbCrLf
    result = result & "平均" & vbTab & ave & vbCrLf
    result = result & "最頻" & vbTab & hin & vbCrLf
    
    ' 結果をクリップボードにコピー
    On Error Resume Next
    With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        .SetText result
        .PutInClipboard
    End With
    
    MsgBox "結果をクリップボードにコピーしました。", vbInformation
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ

#Google Apps Script #Google Drive #スプレッドシート #ExcelVBA #VBAマクロ #文字数カウント #データ分析 #業務効率化 #Excel活用 #データ処理 #クリップボードコピー #統計解析 #辞書オブジェクト #マクロ #スクリプト #プログラミング初心者 #業務改善 #エクセル自動化 #データ集計 #最頻値


英語訳

This explanation was created using ChatGPT.

Script Name: Column Text Length Analysis Tool

This script utilizes Excel VBA (Visual Basic for Applications) to analyze the number of characters in the selected column.

How the Script Works

  1. Retrieve Data from the Selected Column

    • The script retrieves all data from the currently selected column and stores it in an array.

  2. Calculate the Number of Characters in Each Cell

    • It loops through the data and calculates the number of characters in each cell.

  3. Compute Statistical Data

    • Minimum Value (Shortest Text Length)

    • Maximum Value (Longest Text Length)

    • Average (Mean Character Length)

    • Mode (Most Frequent Character Length)

  4. Copy the Results to the Clipboard

    • The calculated results are copied to the clipboard, allowing you to easily paste them into Excel or another application.

  5. Display a Message Box Notification

    • A message box appears stating "The results have been copied to the clipboard." to confirm the process is complete.

Key Features

  • Disabling Screen Updating temporarily enhances processing speed.

  • Dictionary Object Usage efficiently calculates the mode.

  • Error Handling (On Error Resume Next) prevents script crashes during clipboard operations.

How to Use

  1. Select the column in Excel that you want to analyze (only one column).

  2. Run the macro, and it will analyze the text lengths in the selected column.

  3. The results are copied to the clipboard, and you can paste them using Ctrl + V.

Use Cases

  • Analyzing character lengths in customer name lists

  • Checking the statistical distribution of product descriptions

  • Evaluating character counts in survey responses


Related Links

Hashtags

#GoogleAppsScript #GoogleDrive #Spreadsheet #ExcelVBA #VBAMacro #CharacterCount #DataAnalysis #Efficiency #ExcelUsage #DataProcessing #ClipboardCopy #StatisticalAnalysis #DictionaryObject #Macro #Script #ProgrammingForBeginners #WorkOptimization #ExcelAutomation #DataAggregation #ModeValue

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