'変数宣言強制 Option Explicit 'モジュール変数 Private mMoney As Long Private mData() As Long '計算ボタン Private Sub CommandButton1_Click() Dim txt As MSForms.TextBox Set txt = Me.TextBox1 If IsNumeric(txt.Value) = False Then Call MsgBox("数字を入力してください。", vbExclamation + vbOKOnly, "注意") Me.TextBox1.SelStart = 0 Me.TextBox1.SelLength = Len(Me.TextBox1.Text) Me.TextBox1.SetFocus Exit Sub End If mMoney = CLng(txt.Value) Call CalcData Call W_WS txt.Text = "" Set txt = Nothing Call Me.TextBox1.SetFocus End Sub '閉じるボタン Private Sub CommandButton2_Click() Call Unload(Me) End Sub 'フォームインスタンス Private Sub UserForm_Initialize() '配列再定義 ReDim mData(10) As Long 'データボック開く Dim cls As Class2 Set cls = New Class2 Call cls.AddWB 'クラス解放 Set cls = Nothing End Sub 'フォーム閉じる Private Sub UserForm_Terminate() '合計 Call Sum_Kinsyu '配列開放 If IsEmpty(mData) = True Then Erase mData 'データファイル保存 '同名ファイルエラートラップ(上書きする) Dim fs As Object Set fs = Application.FileSearch Dim dir_str As String dir_str = CurDir() Call ChDir(dir_str) fs.LookIn = dir_str fs.Filename = gStr If fs.Execute > 0 Then Dim i As Integer Dim cnt As Integer cnt = fs.FoundFiles.Count If cnt > 0 Then Call Kill(dir_str & "\" & gStr) End If Call gWB.SaveAs(gStr) End Sub '金種計算 Private Sub CalcData() '金種計算クラス Dim cls As Class1 Set cls = New Class1 Call cls.GetData(mMoney, mData) 'クラス解放 Set cls = Nothing End Sub 'ワークシートに書き込む Private Sub W_WS() '最終行 Dim r As Long '書き込みクラス Dim cls As Class2 Set cls = New Class2 r = gWS.Cells(65536, 1).End(xlUp).Row + 1 Call cls.WriteData(r, mData) 'クラス開放 Set cls = Nothing End Sub '合計額 Private Sub Sum_Kinsyu() '最終行 Dim r As Long r = gWS.Cells(65536, 1).End(xlUp).Row + 1 gWS.Cells(r, 1).Value = "合計" '合計額を格納する配列 Dim a() As Long ReDim a(9) As Long Dim i As Integer Dim j As Long '合計 For j = 2 To r - 1 For i = 0 To 9 a(i) = a(i) + CLng(gWS.Cells(j, i + 2).Value) Next Next '書き出し For i = 0 To 9 gWS.Cells(r, i + 2).Value = a(i) Next '配列開放 If IsEmpty(a) = True Then Erase a End Sub