2017-11-27 5 views
0

クラスモジュールを使用して、保存ボタンのコレクションをすべて同じようにしています。しかし、変数を必要とするサブを実行させようとすると、変数を渡すことができません。クラスモジュールでどのようにパブリック変数を使用しますか?

@ Teaselのプロパティに関するアドバイスを使用して編集されました。この問題は、Let PropertyがModule1から変数を設定することを許可していないようです。

Class1の

Public WithEvents SaveBtn As MSForms.CommandButton 

Dim currentrow As Long 

Private Sub SaveBtn_Click() 
    SendMessage 
    `Even if I just have it Msgbox currentrow it returns 0 
End Sub 

Property Let GetRow(myrow As Long) 
    currentrow = myrow 
End Property 

Property Get GetRow() As Long 
    GetRow = currentrow 
End Property 

Module1の

`Trying to send the value into the Class using Let 
Private Sub SendRow_Click() 
Module1.GetRow = 22 
End Sub 

`Trying to Get the value back from the Class 
Public Sub SendMessage() 
    Dim therow As Long 
    therow = Module1.GetRow 
    `I get the "Method or Data Member not found" error in the line above 
    MsgBox therow 
End Sub 

UserForm1で

`This part works fine 
Dim colSaveButtons As New Collection 

Private Sub UserForm_Initialize() 

Dim i As Long 
Dim ctl As MSForms.Control 
Dim obEvents As Class1 

For Each ctl In Me.Controls 
    If TypeOf ctl Is MSForms.CommandButton Then 
     For i = 0 To 5 
      If ctl.Name = "btnSavePage" & i Then 
       Set obEvents = New Class1 
       Set obEvents.SaveBtn = ctl 
       colSaveButtons.Add obEvents 
      End If 
     Next 
    End If 
Next ctl 
End Sub 
+4

問題を再現するために必要なコードのすべての部分を投稿してください。 [MCVE](https://stackoverflow.com/help/mcve)をチェックしてください。 – omegastripes

+0

何も出力したり返したりするコードはありませんか? –

+0

あなたはかなりの量のコードを与えてくれました。私が言うことは、 'GenericSave'への引数である' var'をブール値として定義したことです。論理的な真偽以外のものを渡そうとすると、期待していない結果が得られます。 –

答えて

1

は、あなたのクラスモジュールに "currentRowの" フィールドを追加します。で

Public WithEvents SaveBtn As MSForms.CommandButton 
Public CurrentRow As Long '<< add this 

Private Sub SaveBtn_Click() 
    SendMessage CurrentRow 
End Sub 

をあなたのループ:

... 
If ctl.Name = "btnSavePage" & i Then 
    Set obEvents = New Class1 
    obEvents.CurrentRow = 10 'or whatever... 
    Set obEvents.SaveBtn = ctl 
    colSaveButtons.Add obEvents 
End If 
... 

そして、あなたのSendMessage方法:

Public Sub SendMessage(CurrentRow As Long) 
    MsgBox "This works" 
End Sub 
+0

ありがとうございました!それは動作します。 – bigbucky

+0

話が早すぎます。 UserForm_InitializeサブラインにCurrentRowの値を設定すると動作します。しかし、別のUserformサブまたは別のモジュールに設定しようとすると、動作しないようです。 – bigbucky

+0

オブジェクトを作成した後に変数の値を調整する必要がある場合は、別のアプローチが必要です。 –

1

あなたはそれを達成するには、2つの貴様の方法を使用することができます。


1.公開プロパティ

簡単にアクセスするには、あなたの変数の値は、プロパティを取得し、あなたはレッツプロパティを必要とし、その値を設定する必要があります。お使いのモジュールで

'Your module private variable 
Dim nameOfYourModuleVariable As String 

... 

'Set property to assign a value to your variable 
Public Property Let nameOfYourProperty(value As String) 
    nameOfYourModuleVariable = value 
End Property 

'Get property to return the value of your variable 
Public Property Get nameOfYourProperty() As String 
    nameOfYourProperty = nameOfYourModuleVariable 
End Property 

あなたは、このようにそれを使用することができます:

'Set the value 
MyModule.nameOfYourProperty = "foo" 

'Get the value 
MyModule.nameOfYourProperty 

は、私は非常にあなたが単にpublicとして、あなたの変数を設定することができますが、そのようなことを行うためにプロパティを使用することをお勧めしますポイント2に示されるように。


2.公共変数

公共に変数を定義すると、どこからでもアクセスできます。お使いのモジュールで

Public nameOfYourVariable As String 

別のモジュールから値を取得または設定します。

'Set the value 
MyModule.nameOfYourVariable = "foo" 

'Get the value 
MyModule.nameOfYourVariable 
+0

Publicプロパティがクラスモジュールに正しく入りますか?だから私が "Module1"のサブに変数を取得したいのであれば、私は 'Module1.nameOfYourGetter =' UserFormの変数をクラスモジュールに取り込むためにLetプロパティを必要としませんか? – bigbucky

+0

@bigbucky実際、私はゲッターを投稿しただけですが、Letプロパティが必要なプロパティを介して変数に値を与えることができます。あなたの例では、私はLetプロパティを投稿しなかったので、あなたはそれにアクセスしたがっているように見えました。私はすぐに私の答えを編集します。 – Teasel

+0

私は参照してください。私はLetプロパティが必要とは思わない。しかし、私はModule1に "currentrow"変数を取得しようとすると動作していないようです。 – bigbucky

関連する問題