简体   繁体   English

使用用户窗体运行宏

[英]Using Userform to run a macro

I did up a Userform and a macro. 我做了一个用户窗体和一个宏。 My problem is I do not know how to combine them together. 我的问题是我不知道如何将它们组合在一起。 The Userform should come out and allow the user to type in the relevant data needed on the click of the command button. 用户表单应该出现,并允许用户单击命令按钮来键入所需的相关数据。 Thereafter, it will run the macro I have written. 之后,它将运行我编写的宏。 How do i do it? 我该怎么做?

Macro for 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

Macro written: 宏写成:

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

Thanks! 谢谢!

Remove Userform3.hide in UserForm_Click() 删除UserForm_Click()中的Userform3.hide

Instead of putting macro in Command1_Click() Just put Userform3.hide 而不是将宏放在Command1_Click()中,只需将Userform3.hide放入

Put in new macro inside the module and try to execute the Macro Like this 在模块中放入新的宏,然后尝试执行宏

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM