Faraz Atarodi
Faraz Atarodi

Reputation: 11

Exchange values between userform and main module in vba

I've been working on this problem for hours now, so I'll really appreciate any answers.

I have a userform with three buttons and I like them to pass a value, so I can use that value in my main module and depending on the value, run a specific code. I searched everywhere but they all pass values from textboxs

Here is my userform code:

private sub cancelButton_Click()
    response = 1
    UserForm1.Hide
End Sub

private Sub SutunButton_Click()
    response = 2
    UserForm1.Hide
End Sub

private Sub TirButton_Click()
    response = 3
    UserForm1.Hide
End Sub

and this is my main module:

public response as integer
sub example()
.
.
.
userform1.show
if response=1 then
msgbox "1"
elseif response = 2 then
msgbox "2"
elseif response = 3 then
msgbox "3"
end if
.
.
.
end sub

Of course I put msgbox to make my code simple.

Thanks for any help

Upvotes: 1

Views: 140

Answers (1)

Alex K.
Alex K.

Reputation: 175936

Remove the global. To the module add:

Public Sub process(form As UserForm1, response As Integer)
    form.Hide

    Select Case response
        Case 1: MsgBox 1
        Case 2: MsgBox 2
        Case 3: MsgBox 3
    End Select
End Sub

Change the events to:

private Sub SutunButton_Click()
    process Me, 2
End Sub

Upvotes: 1

Related Questions