使用Userform运行宏

时间:2014-07-21 09:02:21

标签: excel excel-vba userform vba

我做了一个Userform和一个宏。我的问题是我不知道如何将它们组合在一起。 Userform应该出来并允许用户在单击命令按钮时键入所需的相关数据。此后,它将运行我写的宏。我该怎么做?

用户形式的宏:

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

宏写道:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

在UserForm_Click()

中删除Userform3.hide

而不是将宏放在Command1_Click()中,只需输入Userform3.hide

即可

在模块中放入新宏并尝试执行宏 喜欢这个

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

宏像这样

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