showInputsDialog
は、フォームのメンバーではないので、あなたがエラーを取得しているため
Option Explicit
Sub sportUserForm()
Dim sSport As String, sPreference As String
If sportsUsrFrm.showInputsDialog(sSport, sPreference) Then
MsgBox "Your favorite sport is " & sSport & ", and you usually " _
& sPreference & "."
Else
MsgBox "Sorry you don't want to play."
End If
End Sub
Public Function showInputsDialog(sSports As String, sPreference As String) As Boolean
Call Initialize
Me.Show
If Not cancel Then
If optBaseball.Value Then sSport = "Baseball"
ElseIf optBasketball.Value Then sSport = "Basketball"
Elss sSport = "Football"
End If
If optTV.Value Then sPreference = "watch on TV" _
Else: sPreference = "go to games"
End If
showInputsDialog = Not cancel
Unload Me
End Function
ユーザーフォームのコードでは、それはあなたが呼んでいるモジュールのメンバーですそれから。また、この2行でコンパイルエラーが発生するはずです...
Call Initialize
Me.Show
...モジュールとフォームコードが混ざっているようです。
つまり、あなたはこれを考えすぎています。ユーザーフォームはクラスモジュールであり、変数(またはこの場合はWith
ブロック)に格納することができ、プロパティを持つことができます。
'In sportsUsrFrm
Option Explicit
Private mCancel As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = mCancel
End Property
Private Sub cmdCnl_Click()
Me.Hide
mCancel = True
End Sub
Private Sub cmdOK_Click()
If Valid Then Me.Hide '<-- You still need to implement `Valid`
End Sub
をし、このようにそれを呼び出す:私は、フォームにCancelled
プロパティを追加したい
Sub sportUserForm()
With New sportsUsrFrm
.Show
Dim sSport As String, sPreference As String
If Not .Cancelled Then
If .optBaseball.Value Then
sSport = "Baseball"
ElseIf .optBasketball.Value Then
sSport = "Basketball"
Else
sSport = "Football"
End If
If .optTV.Value Then
sPreference = "watch on TV"
Else
sPreference = "go to games"
End If
MsgBox "Your favorite sport is " & sSport & ", and you usually " _
& sPreference & "."
Else
MsgBox "Sorry you don't want to play."
End If
End With
End Sub
どのモジュールには何のコードですか?すべてがユーザーフォームにありますか?フォーム処理のより良いアプローチについては、ドキュメントの[この例](http://stackoverflow.com/documentation/vba/5351/user-forms/19036/best-practices#t=201610252259347492268)を見てみることをお勧めします。 – Comintern
@Comintern質問を反映するためにOPを編集しました。 –
'sportsUsrFrm_Initialize'のコードを追加できますか? – Comintern