Usar Userform para ejecutar una macro

Hice una Userform y una macro. Mi problema es que no sé cómo combinarlos. La forma de usuario debería aparecer y permitir al usuario ingresar los datos relevantes necesarios onclick en el button de command. A partir de entonces, ejecutará la macro que he escrito. ¿Cómo lo hago?

Macro para la forma de usuario:

Private Sub UserForm_Click() Sub Okay_Click() Dim ID1 As String, ID2 As String ID1 = UserForm3.TextBox1.Value If Len(ID1 & vbNullString) = 0 Then MsgBox "Box A is empty" Exit Sub End If ID2 = UserForm3.TextBox2.Value If Len(ID2 & vbNullString) = 0 Then MsgBox "Box B is empty" Else Exit Sub End If UserForm3.Hide End Sub Private Sub TextBox1_Change() Dim ID1 As String ID1 = UserForm3.TextBox1.Value End Sub Private Sub TextBox2_Change() Dim ID2 As String ID2 = UserForm3.TextBox2.Value End Sub 

Macro escrita:

 Sub CommandButton1_Click() Dim SO As String Dim Balance As Integer Dim Source As Worksheet, Target As Worksheet Dim ItsAMatch As Boolean Dim i As Integer Dim ID1 As String Dim ID2 As String UserForm3.Show Set Source = ThisWorkbook.Worksheets("NZ Generic Stock") Set Target = ThisWorkbook.Worksheets("Stock (Data)") SO = Source.Range("A3") Balance = Source.Range("I16") Do Until IsEmpty(Target.Cells(2 + i, 4)) ' This will loop down through non empty cells from row 2 of column 4 If Target.Cells(2 + i, 4) = SO Then ItsAMatch = True Target.Cells(2 + i, 5) = Balance ' This will overwrite your "Balance" value if the name was already in the column Exit Do End If i = i + 1 Loop ' This will write new records if the SO hasn't been already found If ItsAMatch = False Then Target.Cells(1, 4).End(xlDown).Offset(1, 0) = SO Target.Cells(1, 5).End(xlDown).Offset(0, 1) = Balance End If Set Source = Nothing Set Target = Nothing End Sub 

¡Gracias!

Eliminar Userform3.hide en UserForm_Click ()

En lugar de poner macro en Command1_Click () Simplemente ponga Userform3.hide

Ponga una nueva macro dentro del module e intente ejecutar la macro como esta

 Private Sub UserForm_Click() Sub Okay_Click() Dim ID1 As String, ID2 As String ID1 = UserForm3.TextBox1.Value If Len(ID1 & vbNullString) = 0 Then MsgBox "Box A is empty" Exit Sub End If ID2 = UserForm3.TextBox2.Value If Len(ID2 & vbNullString) = 0 Then MsgBox "Box B is empty" Else Exit Sub End If End Sub Private Sub TextBox1_Change() Dim ID1 As String ID1 = UserForm3.TextBox1.Value End Sub Private Sub TextBox2_Change() Dim ID2 As String ID2 = UserForm3.TextBox2.Value End Sub Sub CommandButton1_Click() UserForm3.Hide End Sub 

Macro Like this

 Sub MyMacro() Dim SO As String Dim Balance As Integer Dim Source As Worksheet, Target As Worksheet Dim ItsAMatch As Boolean Dim i As Integer Dim ID1 As String Dim ID2 As String UserForm3.Show Set Source = ThisWorkbook.Worksheets("NZ Generic Stock") Set Target = ThisWorkbook.Worksheets("Stock (Data)") SO = Source.Range("A3") Balance = Source.Range("I16") Do Until IsEmpty(Target.Cells(2 + i, 4)) ' This will loop down through non empty cells from row 2 of column 4 If Target.Cells(2 + i, 4) = SO Then ItsAMatch = True Target.Cells(2 + i, 5) = Balance ' This will overwrite your "Balance" value if the name was already in the column Exit Do End If i = i + 1 Loop ' This will write new records if the SO hasn't been already found If ItsAMatch = False Then Target.Cells(1, 4).End(xlDown).Offset(1, 0) = SO Target.Cells(1, 5).End(xlDown).Offset(0, 1) = Balance End If Set Source = Nothing Set Target = Nothing End Sub