Move code form Userform to Module

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Code Userform

Private Sub CommandButton1_Click()
If Range("A1") = 1 Then
Range("A1").Value = TextBox1.Value
Else
Unload Me
End If
End Sub


Now I just move all the code to Userform. then from Userform call Module

Private Sub CommandButton1_Click()
aaa
End Sub


Sub aaa()
If Range("A1") = 1 Then
Range("A1").Value = TextBox1.Value
Else
Unload Me
End If
End Sub


I did the above but the code doesn't work. Thanks to everyone for help. thank
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
with the right name of your userform
VBA Code:
Range("A1").Value = Userform1.TextBox1.Value
 
Upvote 0
Another way, you can pass information about the currently executing instance of a class (your userform) to a procedure in another module using the ME keyword.

The Me keyword allows you to refer to a current worksheet, workbook, or userform without having to hard code the objects name and represents the parent object from where the code resides. You cannot though as you have discovered, use the Me keyword in a standard module.

Your updated procedure in a STANDARD module (with a parameter added to allow an argument to be passed to it.)

Code:
Sub aaa(ByVal Form As Object)
    If Range("A1") = 1 Then
        Range("A1").Value = Form.TextBox1.Value
    Else
        Unload Form
    End If
End Sub

You call this procedure from your userform commandbutton code and pass the current instance of the UserForm class as an argument using the ME keyword

Code:
Private Sub CommandButton1_Click()
    aaa Me
End Sub

Your code should now do what you want.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top