Excel VBAプログラムで自動データ抽出・集計:顧客名、注文日付、商品名、注文数、合計金額をCSVファイルに保存

python

要約

Excel VBA を使った自動化によって、企業の作業効率が飛躍的に向上している例を見てみましょう。

下記のプログラムは、膨大な量のデータを含む Excel ファイルを自動的に解析し、必要な情報を抽出するものです。

このプログラムによって、従来は手作業で1日以上かかっていた作業が、わずか数秒で完了するようになりました。

企業の生産性向上につながる自動化プログラムの一例をご覧ください。

詳細内容

この Excel VBA プログラムは、膨大な量のデータを含む Excel ファイルを自動的に解析し、必要な情報を抽出するものです。

Excel ファイルには、顧客の氏名、電話番号、住所、注文商品、注文日付などが含まれています。

まず、プログラムは、Excel ファイルを開き、必要なシートを選択します。

次に、プログラムは、データのある最初の行を特定し、その列の見出しを取得します。

それから、プログラムは、必要な情報を含む列を特定し、その列から必要なデータを抽出します。

必要な情報が複数の列にまたがっている場合は、複数の列からデータを抽出します。

次に、プログラムは、データを処理し、顧客ごとに集計します。

集計されたデータは、新しい Excel ファイルに書き込まれます。

このファイルには、顧客ごとに独自の行があり、顧客名、注文商品、注文数、注文日付、および合計金額が含まれています。

このプログラムを実行するには、Excel ファイルを指定する必要があります。

その後、プログラムは、自動的にそのファイルを解析して、抽出する情報を特定します。

抽出されたデータは、CSV 形式の新しい Excel ファイルに保存されます。

このプログラムを使用することで、手作業で多くの時間と費用をかけなければならなかった作業を自動化し、企業の生産性を向上させることができます。

以下に、このプログラムのコードを示します。

“`
Sub extractData()
Dim wb As Workbook
Dim ws As Worksheet
Dim lastRow As Long
Dim rangeToCheck As Range
Dim cell As Range
Dim customerName As String
Dim address As String
Dim phone As String
Dim orderDate As Date
Dim orderItem As String
Dim orderQty As Integer
Dim orderTotal As Double
Dim outputRow As Long

‘Open the workbook and select the sheet to work on
Set wb = Workbooks.Open(“C:\path\to\file.xlsx”)
Set ws = wb.Worksheets(“Sheet1”)

‘Find the first row with data and get the column headers
lastRow = ws.Cells.Find(“*”, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rangeToCheck = ws.Range(“1:” & lastRow)

For Each cell In rangeToCheck.Rows(1).Cells
Select Case LCase(cell.Value)
Case “customer name”
customerName = Split(cell.Address, “$”)(1)
Case “address”
address = Split(cell.Address, “$”)(1)
Case “phone”
phone = Split(cell.Address, “$”)(1)
Case “order date”
orderDate = Split(cell.Address, “$”)(1)
Case “order item”
orderItem = Split(cell.Address, “$”)(1)
Case “order qty”
orderQty = Split(cell.Address, “$”)(1)
Case “order total”
orderTotal = Split(cell.Address, “$”)(1)
End Select
Next cell

‘Process the data for each customer
outputRow = 1
For i = 2 To lastRow
If ws.Range(customerName & i).Value <> “” Then
‘New customer found
outputRow = outputRow + 1
ws.Range(“A” & outputRow).Value = ws.Range(customerName & i).Value
ws.Range(“D” & outputRow).Value = ws.Range(phone & i).Value
ws.Range(“B” & outputRow).Value = ws.Range(address & i).Value
End If

‘Process the order
ws.Range(“E” & outputRow).Value = ws.Range(“E” & outputRow).Value & “, ” & ws.Range(orderItem & i).Value
ws.Range(“F” & outputRow).Value = ws.Range(“F” & outputRow).Value + ws.Range(orderQty & i).Value
ws.Range(“G” & outputRow).Value = ws.Range(“G” & outputRow).Value + ws.Range(orderTotal & i).Value
Next i

‘Save the output file as CSV
wb.SaveAs “C:\path\to\output.csv”, FileFormat:=xlCSV, CreateBackup:=False

‘Close the workbook
wb.Close
End Sub
“`

コメント

タイトルとURLをコピーしました