Excel VBAを使用することで、さまざまな日常業務を効率化することができます。この記事では、Excel VBAを用いて「顧客属性別のリピート購入率」を集計する方法を詳しく解説します。コードを通じて、基本の処理手順から応用例まで、実用的な情報を得ることができます。
基本の処理:顧客属性別のリピート購入率の集計
リピート購入率は、顧客が再度商品やサービスを購入する確率を示す指標です。これを顧客属性別に集計することで、特定の属性の顧客が再購入しやすいか、あるいは離れやすいかの傾向を把握することができます。
Sub RepeatPurchaseRate()
Dim lastRow As Long
Dim countTotal As Long, countRepeat As Long
Dim repeatRate As Double
Dim ws As Worksheet
Dim rng As Range, cell As Range
'ワークシート設定
Set ws = ThisWorkbook.Sheets("Sheet1")
'最終行取得
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'顧客属性別集計
Set rng = ws.Range("A2:A" & lastRow)
For Each cell In rng
If cell.Value = "属性A" Then
countTotal = countTotal + 1
If cell.Offset(0, 1).Value = "リピート" Then
countRepeat = countRepeat + 1
End If
End If
Next cell
'リピート購入率計算
repeatRate = countRepeat / countTotal
'結果出力
ws.Cells(2, 4).Value = "属性Aのリピート購入率"
ws.Cells(2, 5).Value = repeatRate
End Sub
このコードは、属性Aの顧客の中で再購入をした顧客の数をカウントし、リピート購入率を計算しています。結果はD2とE2のセルに出力されます。
応用例1: 他の属性のリピート購入率も集計
もちろん、属性Aだけでなく、他の属性についても同様の集計を行うことができます。以下は、属性Bのリピート購入率を集計する例です。
'属性Bの顧客に絞り込む
If cell.Value = "属性B" Then
countTotal = countTotal + 1
If cell.Offset(0, 1).Value = "リピート" Then
countRepeat = countRepeat + 1
End If
End If
応用例2: 複数属性のリピート購入率を集計
複数の属性にわたるリピート購入率を一度に集計する方法を紹介します。
Sub MultiAttributeRepeatRate()
' ... [先のコード部分を継続]
Dim attr As Variant
Dim attrList As Variant
attrList = Array("属性A", "属性B", "属性C")
For Each attr In attrList
'初期化
countTotal = 0
countRepeat = 0
For Each cell In rng
If cell.Value = attr Then
countTotal = countTotal + 1
If cell.Offset(0, 1).Value = "リピート" Then
countRepeat = countRepeat + 1
End If
End If
Next cell
'結果出力
ws.Cells(2 + Application.Match(attr, attrList, 0), 4).Value = attr & "のリピート購入率"
ws.Cells(2 + Application.Match(attr, attrList, 0), 5).Value = countRepeat / countTotal
Next attr
End Sub
応用例3: リピート購入率の高い属性をハイライト
リピート購入率が一定の基準以上の属性をハイライトする方法です。
Sub HighlightHighRepeatRate()
' ... [先のコード部分を継続]
Dim targetCell As Range
For Each targetCell In ws.Range("E2:E" & 2 + UBound(attrList))
If targetCell.Value >= 0.5 Then '50%以上の場合
targetCell.Interior.Color = RGB(255, 0, 0) '赤色にハイライト
End If
Next targetCell
End Sub
この例では、リピート購入率が50%以上の属性を赤色でハイライトしています。
まとめ
Excel VBAを活用すれば、顧客属性別のリピート購入率を効率よく集計することができます。基本の手順から応用例まで、さまざまなシチュエーションに応じた方法を取り入れて、日常業務を更に効率化しましょう。
コメント