プロが教えるわが家の防犯対策術!

ユーザーフォームfrmMedicalInfoEntryに日付入力用のテキストボックスtxtDateとコマンドボタンbtnDispCalenderと配置して、カレンダーユーザーフォームfrmCalenderで作った、カレンダーから日付を入力するため、クラスモジュールclsCalenderにコードを以下のとおり追加して、実行しようとすると、「変数arryClass(c)が定義されていません」とエラーメッセージが出てプロシージャが止まってしまいます。何処が間違っているのでしょうか?ちなみに「’」のところはカレンダーが表示されないのでコメントアウトしているところです。なお、字数がオーバーするので、同一質問②で送ります。よろしくご教授ください。

①frmCalenderのコードは以下のとおりです。
Private arrayClass() As New clsCalender
'Public EntryControl As String
'Public RarentForm As UserForm
Private Sub cmbMonth_Change()
If CheckParam() Then
makeCalender cmbYear.Value, cmbMonth.Value
End If
End Sub
Private Sub cmbYear_Change()
If CheckParam() Then
makeCalender cmbYear.Value, cmbMonth.Value
End If
End Sub

Private Sub SpinButton1_SpinDown()
If cmbMonth.Value = 1 Then
cmbMonth = 12
cmbYear.Value = cmbYear - 1
Else
cmbMonth = cmbMonth - 1
End If
End Sub

Private Sub SpinButton1_SpinUp()

If cmbMonth.Value = 12 Then
cmbMonth = 1
cmbYear.Value = cmbYear + 1
Else
cmbMonth = cmbMonth + 1
End If
End Sub

Private Sub UserForm_Initialize()
Dim lbl As MSForms.Label
Dim i As Integer, j As Integer, c As Integer
Dim arrayClass() As Variant
Dim Weekdays() As Variant 'データ型に注意
Weekdays() = Array("日", "月", "火", "水", "木", "金", "土")
For i = 1 To 7
Set lbl = Me.Controls.Add("Forms.Label.1", , True)
With lbl
.Caption = Weekdays(i - 1)
.Width = 15
.Height = 15
.Left = 5 + (.Width + 2) * (i - 1)
.Top = 20
.BorderColor = &H666666
.BorderStyle = fmBorderStyleSingle
.Font.Size = 11
End With
Next
c = 1
For i = 1 To 6 '縦方向
For j = 1 To 7 '横方向
Set lbl = Me.Controls.Add("Forms.Label.1", , True)
With lbl
.Width = 15
.Height = 15
.Left = 5 + (j - 1) * (.Width + 2)
.Top = 20 + i * (.Height + 2)
.BorderColor = &H666666
.BorderStyle = fmBorderStyleSingle
.Font.Size = 11
.TextAlign = fmTextAlignRight
.Name = "D" & c
' ReDim Preserve arryClass(c)
' arrayClass(c).NewCalender lbl, Me
c = c + 1
End With
Next
Next
For i = Year(Date) - 3 To Year(Date) + 3
cmbYear.AddItem i
Next
For i = 1 To 12
cmbMonth.AddItem i
Next
'コンボボックスに値を代入すると、Changeイベントが発生する
cmbYear.Value = Year(Date)
cmbMonth.Value = Month(Date)
'Public変数の呼び出し元のフォームを格納
' Set ParentForm = frmMedicalInfoEntry
End Sub
Sub makeCalender(Nen As Integer, Tsuki As Integer)

Dim i As Integer, c As Integer
Dim WeekDay_of_firstday As Integer
Dim lastDay As Integer
Dim myDate As Date
'ラベルの表示をクリア
For i = 1 To 42
Controls("D" & i).Caption = ""
Next
c = 1
WeekDay_of_firstday = getWeeekday1st(Nen, Tsuki)
lastDay = Day(DateSerial(Nen, Tsuki + 1, 1) - 1) - 1
For i = WeekDay_of_firstday To WeekDay_of_firstday + lastDay
Controls("D" & i).Caption = c
Controls("D" & i).Tag = CStr(DateSerial(Nen, Tsuki, c))
Controls("D" & i).ForeColor = vbBlack
c = c + 1
Next
'前月の日付の設定
myDate = DateSerial(Nen, Tsuki, 1) - 1
c = Day(myDate)
For i = WeekDay_of_firstday - 1 To 1 Step -1
Controls("D" & i).Caption = c
Controls("D" & i).Tag = CStr(DateSerial(Year(myDate), Month(myDate), c))
Controls("D" & i).ForeColor = RGB(100, 100, 100)
c = c - 1
Next
'来月の日付の設定
myDate = DateSerial(Nen, Tsuki + 1, 1)
c = 1
For i = WeekDay_of_firstday + lastDay + 1 To 42
Controls("D" & i).Caption = c
Controls("D" & i).Tag = CStr(DateSerial(Year(myDate), Month(myDate), c))
Controls("D" & i).ForeColor = RGB(100, 100, 100)
c = c + 1
Next
End Sub

質問者からの補足コメント

  • うーん・・・

    実際にはどう書き変えればいいのでしょうか? まだ初心者なので、教えていただければ助かります。よろしくお願いします。

    No.1の回答に寄せられた補足コメントです。 補足日時:2023/02/17 20:28

A 回答 (1件)

フォームのprivateメンバー変数arrayClass()が、UserForm_Initialize関数内の一時変数arrayClass()により隠されてて、実際にはclsCalenderがarrayClass()の要素としてインスタンス化されてないように見えます。



明示的にNewするコードにしてみては。
この回答への補足あり
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!