Excel(エクセル)実用編:家計簿の作成(VBA) |
||
| スポンサードリンク | |
| はじめに | データの入力&訂正 | 並べ替え | 月別集計 |
| 項目別集計 | 年間集計 | 購入店別集計 | |
| ダウンロード kakei_D5.xls(約200KB) | |||
| Private Sub UserForm_Initialize() Dim r As Long Dim myDCount As Long, myDRange As String Dim 最終行 As Long, 項目行 As Long Application.ScreenUpdating = False 最終行 = Worksheets("入力表").Range("B" & Rows.count).End(xlUp).Row TBoxNo.Value = 最終行 - 10 + 1 TBox年月日.Value = Date TBox収入.Value = Null TBox支出.Value = Null 項目行 = Worksheets("項目一覧表").Range("B" & Rows.count).End(xlUp).Row CBox項目.RowSource = "項目一覧表!B3:B" & 項目行 項目行 = Worksheets("項目一覧表").Range("G" & Rows.count).End(xlUp).Row CBox購入店.RowSource = "項目一覧表!G3:G" & 項目行 TBox適用.Value = Null Application.ScreenUpdating = True Worksheets("入力表").Activate Cells(最終行 + 1, 2).Select End Sub |
| ’----[追加・修正]ボタンで実行 Private Sub CButtonOK_Click() Dim r As Long, 最終行 As Long, 項目行 As Long Dim re As String r = TBoxNo.Value + 10 最終行 = Worksheets("入力表").Range("B" & Rows.count).End(xlUp).Row If r <= 最終行 Then re = MsgBox("訂正" & " " & "すでにデータが入力されています。" & Chr(13) & _ Chr(13) & "データを置き換えます。 本当に良いですか? ", _ Buttons:=vbYesNo + vbExclamation, Title:="注意!!") If re = vbYes Then With Worksheets("入力表") .Cells(r, 2).Activate .Cells(r, 1).Value = TBoxNo.Value .Cells(r, 2).Value = TBox年月日.Value .Cells(r, 3).Value = CBox項目.Value .Cells(r, 4).Value = TBox収入.Value .Cells(r, 5).Value = TBox支出.Value .Cells(r, 6).Value = TBox適用.Value .Cells(r, 7).Value = CBox購入店.Value End With データクリア Exit Sub End If データクリア Exit Sub End If If r >= 最終行 + 1 Then r = 最終行 + 1 End If With Worksheets("入力表") .Cells(r, 1).Value = TBoxNo.Value .Cells(r, 2).Value = TBox年月日.Value .Cells(r, 3).Value = CBox項目.Value .Cells(r, 4).Value = TBox収入.Value .Cells(r, 5).Value = TBox支出.Value .Cells(r, 6).Value = TBox適用.Value .Cells(r, 7).Value = CBox購入店.Value End With データクリア End Sub ’-------------- Sub データクリア() Dim 最終行 As Long 最終行 = Worksheets("入力表").Range("B" & Rows.count).End(xlUp).Row Worksheets("入力表").Activate Cells(最終行 + 1, 2).Select TBoxNo.Value = 最終行 - 10 + 1 TBox年月日.Value = Date TBox収入.Value = Null TBox支出.Value = Null TBox適用.Value = Null CBox項目.Value = Null CBox購入店.Value = Null End Sub ’--------------- Private Sub CButtonキャンセル_Click() Dim 最終行 As Long With Worksheets("入力表") 最終行 = .Range("B" & Rows.count).End(xlUp).Row If 最終行 < 11 Then 最終行 = 11 .Range("A11:G" & 最終行).Interior.ColorIndex = 34 .Range("A11:G" & 最終行).Borders.Weight = xlThin .Range("A11:G" & 最終行).Borders.ColorIndex = 11 End With Me.Hide End Sub |
| Private Sub TBoxNo_afterUpdate() Dim データNo As Long, 最終行 As Long, r As Long データNo = TBoxNo.Value 最終行 = Worksheets("入力表").Range("B" & Rows.count).End(xlUp).Row If データNo <= 最終行 - 10 Then r = データNo + 10 With Worksheets("入力表") .Activate .Cells(r, 2).Select TBoxNo.Value = .Cells(r, 1).Value TBox年月日.Value = .Cells(r, 2).Value CBox項目.Value = .Cells(r, 3).Value TBox収入.Value = Format(.Cells(r, 4).Value, "###,###") TBox支出.Value = Format(.Cells(r, 5).Value, "###,###") TBox適用.Value = .Cells(r, 6).Value CBox購入店.Value = .Cells(r, 7).Value End With Exit Sub End If If データNo > 最終行 - 10 Then データNo = 最終行 - 9 TBoxNo.Value = データNo データクリア End If End Sub ’------------------ Private Sub TBox収入_afterUpdate() Dim 収入 As Long 収入 = TBox収入.Value TBox収入.Value = Format(収入, "###,###") End Sub ’------------------ Private Sub TBox支出_afterUpdate() Dim 支出 As Long 支出 = TBox支出.Value TBox支出.Value = Format(支出, "###,###") End Sub |
| Sub 並べ替え() '入力表のデータを並べ替える Dim lastRow As Long, i As Long, lastClm As Integer With Worksheets("入力表") lastRow = .Range("B" & Rows.count).End(xlUp).Row lastClm = .Cells(10, Columns.count).End(xlToLeft).Column .Range(.Cells(11, 1), .Cells(lastRow, lastClm)).Sort _ Key1:=Range("B11"), _ Order1:=xlAscending, Key2:=Range("C11"), _ Order2:=xlAscending, _ Header:=xlGuess, _ OrderCustom:=1, _ MatchCase:=False, _ Orientation:=xlTopToBottom, _ SortMethod:=xlPinYin, _ DataOption1:=xlSortNormal, _ DataOption2:=xlSortNormal lastRow = .Range("B" & Rows.count).End(xlUp).Row For i = 11 To lastRow .Cells(i, 1).Value = i - 10 Next i .Activate .Cells(lastRow, 1).Select End With End Sub |
| Private Sub CommandButton1_Click() '月別集計 Dim A, 月, 計算月 As Integer Dim i As Long, j As Long, count As Long Dim rangeA As Range Dim 最終行番号 As Long, 最終列番号 As Long, 入力最終行 As Long Dim 月日 As Date Dim 残高 As Long Application.ScreenUpdating = False With Worksheets("月別集計表") .Range("A1").Value = "" .Range("A2").Formula = "=MONTH(入力表!B11)=" & Val(CBox月数.Value) Label1.Caption = "抽出中........ " Frm月別集計.Repaint 最終行番号 = .Range("B" & Rows.count).End(xlUp).Row .Activate .Range("B5").RemoveSubtotal .Range("B6:G" & 最終行番号 + 1).Delete shift:=xlUp 入力最終行 = Sheets("入力表").Range("B" & Rows.count).End(xlUp).Row Sheets("入力表").Range("A10:G" & 入力最終行).AdvancedFilter _ Action:=xlFilterCopy, _ CriteriaRange:=.Range("A1:A2"), _ CopyToRange:=.Range("B5:G5"), _ Unique:=False 最終行番号 = .Range("B" & Rows.count).End(xlUp).Row ' 計算(合計) If 最終行番号 = 5 Then 最終行番号 = 6 .Range("C" & 最終行番号 + 1).Value = "合 計" .Range("D" & 最終行番号 + 1).FormulaR1C1 = "=SUM(R6C4:R[-1]C4)" .Range("E" & 最終行番号 + 1).FormulaR1C1 = "=SUM(R6C5:R[-1]C5)" .Range("B6:G" & 最終行番号 + 1).Borders.Weight = xlThin .Range("B6:B" & 最終行番号 + 1).NumberFormat = "mm月dd日" .Range("D6:E" & 最終行番号 + 1).NumberFormat = "#,##0" .Range("B6:G" & 最終行番号 + 1).Interior.ColorIndex = 36 .Range("B6:G" & 最終行番号).Interior.ColorIndex = 34 .Activate .Range("B" & 最終行番号 + 1).Select End With Application.ScreenUpdating = True Me.Hide End Sub |
| Private Sub CommandButton4_Click() '年間集計 Label1.Caption = "集計中........ " Frm月別集計.Repaint Dim 月(1 To 12), 計算月 As Integer Dim i As Long, j As Long, k As Long, count1 As Integer, count2 As Integer Dim データ行 As Long, データ数 As Long, 最終行 As Long Dim 収入金額 As Long, 支出金額 As Long Dim 収入項目 As String, 支出項目 As String Dim 項目1 As String, 項目3 As String Dim 月1 As String Dim 収入項目名(1 To 20) As String Dim 収入(1 To 20, 1 To 20) As Double, 支出(1 To 20, 1 To 20) As Double Dim 収入1 As Double, 支出1 As Double, x As Double Dim 支出項目名1(1 To 20) As String Dim 合計 As Range Dim checker As Integer, 比較値 As Integer Dim 行頭 As Integer Application.ScreenUpdating = False データ行 = Worksheets("入力表").Range("B" & Rows.count).End(xlUp).Row データ数 = データ行 - 10 '----データ表のクリア With Worksheets("年間集計") 最終行 = .Range("B" & Rows.count).End(xlUp).Row If 最終行 < 3 Then 最終行 = 3 .Range("B3:O" & 最終行).Delete End With '----収入のあった項目名を取得 count1 = 0 For i = 1 To データ数 収入金額 = Worksheets("入力表").Range("D" & i + 10).Value If 収入金額 > 0 Then 収入項目 = Worksheets("入力表").Range("C" & i + 10).Value If count1 = 0 Then count1 = 1 収入項目名(count1) = 収入項目 End If checker = 0 '----項目名が既出か否かを判定 For j = 1 To count1 比較値 = StrComp(収入項目名(j), 収入項目) If 比較値 = 0 Then checker = 1 Exit For End If Next j '----項目名が無いときは追加する If checker = 0 Then count1 = count1 + 1 収入項目名(count1) = 収入項目 End If End If Next i For i = 1 To 12 月(i) = i For j = 1 To count1 For k = 11 To データ行 項目1 = Worksheets("入力表").Cells(k, 3).Value 月1 = Worksheets("入力表").Cells(k, 2).Value 月1 = Month(月1) If 月(i) = 月1 And 項目1 = 収入項目名(j) Then 収入1 = Worksheets("入力表").Cells(k, 4).Value 収入(i, j) = 収入(i, j) + 収入1 End If Next k Next j For j = 1 To count1 Worksheets("年間集計").Cells(j + 4, 2).Value = 収入項目名(j) Worksheets("年間集計").Cells(j + 4, i + 2).Value = 収入(i, j) Next j Next i With Worksheets("年間集計") .Cells(4, 15).Value = "合 計" For i = 1 To count1 x = WorksheetFunction.Sum(.Range(.Cells(4 + i, 3), .Cells(4 + i, 15))) .Cells(4 + i, 15).Value = x Next i .Cells(count1 + 6, 2).Value = "合 計" For i = 3 To 15 x = WorksheetFunction.Sum(.Range(.Cells(4 + count1, i), .Cells(5, i))) .Cells(count1 + 6, i).Value = x Next i '----表の作成 For i = 3 To 14 .Cells(4, i).Value = i - 2 & "月" Next i With .Range("B3") .Value = "収入" .Font.Size = 20 .Font.Bold = True .Font.Name = "HGP行書体" .Font.ColorIndex = 10 End With .Range("B4:O" & count1 + 6).Borders.Weight = xlThin .Range("C5:O" & count1 + 6).NumberFormat = "#,##0" .Range("B4:O" & count1 + 6).Interior.ColorIndex = 36 .Range("B4:O4").Interior.ColorIndex = 40 .Range("B4:O4").HorizontalAlignment = xlCenter .Range("C5:N" & count1 + 5).Interior.ColorIndex = 34 End With '----支出のあった項目名を取得 count2 = 0 For i = 1 To データ数 支出金額 = Worksheets("入力表").Range("E" & i + 10).Value If 支出金額 > 0 Then 支出項目 = Worksheets("入力表").Range("C" & i + 10).Value If count2 = 0 Then count2 = 1 支出項目名1(count2) = 支出項目 End If checker = 0 For j = 1 To count2 比較値 = StrComp(支出項目名1(j), 支出項目) If 比較値 = 0 Then checker = 1 Exit For End If Next j If checker = 0 Then count2 = count2 + 1 支出項目名1(count2) = 支出項目 End If End If Next i For i = 1 To 12 月(i) = i For j = 1 To count2 For k = 11 To データ行 項目3 = Worksheets("入力表").Cells(k, 3).Value 月1 = Worksheets("入力表").Cells(k, 2).Value 月1 = Month(月1) If 月(i) = 月1 And 項目3 = 支出項目名1(j) Then 支出1 = Worksheets("入力表").Cells(k, 5).Value 支出(i, j) = 支出(i, j) + 支出1 End If Next k Next j 行頭 = count1 + 4 + 6 For j = 1 To count2 Worksheets("年間集計").Cells(j + 行頭, 2).Value = 支出項目名1(j) Worksheets("年間集計").Cells(j + 行頭, i + 2).Value = 支出(i, j) Next j Next i With Worksheets("年間集計") .Cells(行頭, 15).Value = "合 計" For i = 1 To count2 x = WorksheetFunction.Sum(.Range(.Cells(行頭 + i, 3), .Cells(行頭 + i, 15))) .Cells(行頭 + i, 15).Value = x Next i .Cells(count2 + 行頭 + 2, 2).Value = "合 計" For i = 3 To 15 x = WorksheetFunction.Sum(.Range(.Cells(行頭 + count2, i), .Cells(行頭, i))) .Cells(count2 + 行頭 + 2, i).Value = x Next i '----表の作成 .Cells(行頭, 2).Resize(, 14).Value = .Range("B4:O4").Value .Cells(行頭, 2).Resize(, 14).Interior.ColorIndex = 40 .Cells(行頭, 2).Resize(, 14).HorizontalAlignment = xlCenter .Cells(行頭 - 1, 2).Value = "支出" .Cells(行頭 - 1, 2).Font.Size = 20 .Cells(行頭 - 1, 2).Font.Bold = True .Cells(行頭 - 1, 2).Font.Name = "HGP行書体" .Cells(行頭 - 1, 2).Font.ColorIndex = 10 .Range(.Cells(行頭, 2), .Cells(行頭 + count2 + 2, 15)).Borders.Weight = xlThin .Range(.Cells(行頭 + 1, 3), .Cells(行頭 + count2 + 2, 15)).NumberFormat = "#,##0" .Range(.Cells(行頭 + 1, 3), .Cells(行頭 + count2 + 2, 14)).Interior.ColorIndex = 34 .Range(.Cells(行頭 + 1, 2), .Cells(行頭 + count2 + 2, 2)).Interior.ColorIndex = 36 .Range(.Cells(行頭 + 1, 15), .Cells(行頭 + count2 + 2, 15)).Interior.ColorIndex = 36 .Range(.Cells(行頭 + count2 + 2, 2), .Cells(行頭 + count2 + 2, 15)).Interior.ColorIndex = 36 .Activate .Cells(1, 1).Select End With Application.ScreenUpdating = True Label1.Caption = "集計が終わりました" Me.Hide End Sub |
| Sub y_syukei() Dim myDic As Object, myKey, myItem Dim myVal, myVal2, myVal3, myVal4(1000, 12), myVal5(1000, 12) Dim i As Long, j As Long, cnt As Long, f As Long Set myDic = CreateObject("Scripting.Dictionary") ' ---元データを配列に格納 With Worksheets("入力表") myVal = .Range("B11:G" & .Range("B" & Rows.count).End(xlUp).Row).Value End With ' **********myDicへ収入データを格納*********** For i = 1 To UBound(myVal, 1) myVal2 = Month(myVal(i, 1)) & "_" & myVal(i, 2) If Not myVal2 = "_" And myVal(i, 3) > 0 Then If Not myDic.exists(myVal2) Then myDic.Add myVal2, myVal(i, 3) Else myDic(myVal2) = myDic(myVal2) + myVal(i, 3) End If End If Next ' ---Key,Itemの書き出し myKey = myDic.keys myItem = myDic.items For i = 0 To UBound(myKey) myVal3 = Split(myKey(i), "_") f = 0 For j = 1 To cnt If myVal4(j, 0) = myVal3(1) Then f = j Exit For End If Next j If f = 0 Then cnt = cnt + 1: f = cnt myVal4(f, 0) = myVal3(1) myVal4(f, myVal3(0)) = myItem(i) Next Set myDic = Nothing ' **********myDicへ支出データを格納*********** Set myDic = CreateObject("Scripting.Dictionary") For i = 1 To UBound(myVal, 1) myVal2 = Month(myVal(i, 1)) & "_" & myVal(i, 2) If Not myVal2 = "_" And myVal(i, 4) > 0 Then If Not myDic.exists(myVal2) Then myDic.Add myVal2, myVal(i, 4) Else myDic(myVal2) = myDic(myVal2) + myVal(i, 4) End If End If Next ' ---Key,Itemの書き出し myKey = myDic.keys myItem = myDic.items cnt = 0 For i = 0 To UBound(myKey) myVal3 = Split(myKey(i), "_") f = 0 For j = 1 To cnt If myVal5(j, 0) = myVal3(1) Then f = j Exit For End If Next j If f = 0 Then cnt = cnt + 1: f = cnt myVal5(f, 0) = myVal3(1) myVal5(f, myVal3(0)) = myItem(i) Next '--------合計の計算と表の作成 Dim lastR As Long, lastR2 As Long Application.ScreenUpdating = False With Worksheets("年間集計") lastR = .Range("B" & Rows.count).End(xlUp).Row If lastR < 5 Then lastR = 5 .Range("3:" & lastR).Delete .Range("B5").Resize(UBound(myVal4, 1) + 1, UBound(myVal4, 2) + 1).Value = myVal4 lastR = .Range("B" & Rows.count).End(xlUp).Row .Range(lastR + 1 & ":2000").Delete .Range("O5").Value = "合 計" For i = 6 To lastR .Cells(i, 15).Value = WorksheetFunction.Sum(.Range(.Cells(i, 3), .Cells(i, 15))) Next i .Range("B" & lastR + 1).Value = "合 計" For i = 3 To 15 .Cells(lastR + 1, i).Value = WorksheetFunction.Sum(.Range(.Cells(6, i), .Cells(lastR, i))) Next i For i = 3 To 14 .Cells(5, i).Value = i - 2 & "月" Next i With .Range("B4") .Value = "収入" .Font.Size = 20 .Font.Bold = True .Font.Name = "HGP行書体" .Font.ColorIndex = 10 End With .Range("B5:O" & lastR + 1).Borders.Weight = xlThin .Range("C6:O" & lastR + 1).NumberFormat = "#,##0" .Range("B5:O" & lastR + 1).Interior.ColorIndex = 36 .Range("B5:O5").Interior.ColorIndex = 40 .Range("B5:O5").HorizontalAlignment = xlCenter .Range("C6:N" & lastR).Interior.ColorIndex = 34 '-----支出の表作成 lastR = .Range("B" & Rows.count).End(xlUp).Row .Range("B" & Rows.count).End(xlUp).Offset(3, 0).Resize(UBound(myVal5, 1) + 1, UBound(myVal5, 2) + 1).Value = myVal5 lastR2 = .Range("B" & Rows.count).End(xlUp).Row .Range(lastR2 + 1 & ":2000").Delete .Cells(lastR + 3, 15).Value = "合 計" For i = lastR + 4 To lastR2 .Cells(i, 15).Value = WorksheetFunction.Sum(.Range(.Cells(i, 3), .Cells(i, 15))) Next i .Range("B" & lastR2 + 1).Value = "合 計" For i = 3 To 15 .Cells(lastR2 + 1, i).Value = WorksheetFunction.Sum(.Range(.Cells(lastR + 3, i), .Cells(lastR2, i))) Next i For i = 3 To 14 .Cells(lastR + 3, i).Value = i - 2 & "月" Next i With .Cells(lastR + 2, 2) .Value = "支出" .Font.Size = 20 .Font.Bold = True .Font.Name = "HGP行書体" .Font.ColorIndex = 10 End With .Range(.Cells(lastR + 3, 2), .Cells(lastR2 + 1, 15)).Borders.Weight = xlThin .Range(.Cells(lastR + 4, 3), .Cells(lastR2 + 1, 15)).NumberFormat = "#,##0" .Range(.Cells(lastR + 3, 2), .Cells(lastR2 + 1, 15)).Interior.ColorIndex = 36 .Range(.Cells(lastR + 3, 2), .Cells(lastR + 3, 15)).Interior.ColorIndex = 40 .Range(.Cells(lastR + 3, 3), .Cells(lastR + 3, 15)).HorizontalAlignment = xlCenter .Range(.Cells(lastR + 4, 3), .Cells(lastR2, 14)).Interior.ColorIndex = 34 .Activate .Range("A3").Select End With Application.ScreenUpdating = True End Sub |
| Private Sub CommandButton5_Click() '購入店別集計 Dim 最終行番号 As Long, 入力最終行 As Long Dim rangeA As Range Application.ScreenUpdating = False With Worksheets("購入店別集計表") .Range("A1").Value = Worksheets("入力表").Range("G10").Value .Range("A2").Formula = "=""=" & CBox購入店.Value & """" Label1.Caption = "抽出中........ " Frm月別集計.Repaint 最終行番号 = .Range("B" & Rows.count).End(xlUp).Row If 最終行番号 < 6 Then 最終行番号 = 6 .Activate .Range("B6:G" & 最終行番号 + 1).Delete shift:=xlUp 入力最終行 = Sheets("入力表").Range("B" & Rows.count).End(xlUp).Row Sheets("入力表").Range("A10:G" & 入力最終行).AdvancedFilter _ Action:=xlFilterCopy, _ CriteriaRange:=.Range("A1:A2"), _ CopyToRange:=.Range("B5:G5"), _ Unique:=False ' 計算(合計) 最終行番号 = .Range("B" & Rows.count).End(xlUp).Row If 最終行番号 < 6 Then 最終行番号 = 6 .Range("C" & 最終行番号 + 1).Value = "合 計" .Range("D" & 最終行番号 + 1).FormulaR1C1 = "=SUM(R6C4:R[-1]C4)" .Range("E" & 最終行番号 + 1).FormulaR1C1 = "=SUM(R6C5:R[-1]C5)" .Range("B6:G" & 最終行番号 + 1).Borders.Weight = xlThin .Range("B6:B" & 最終行番号 + 1).NumberFormat = "mm月dd日" .Range("D6:E" & 最終行番号 + 1).NumberFormat = "#,##0" .Range("B6:G" & 最終行番号 + 1).Interior.ColorIndex = 36 .Range("B6:G" & 最終行番号).Interior.ColorIndex = 34 .Activate .Range("B" & 最終行番号 + 1).Select End With Application.ScreenUpdating = True Me.Hide End Sub |
スポンサードリンク
PageViewCounter
Since2006/2/27