"确定" userform中的命令框

时间:2014-07-18 02:37:37

标签: excel excel-vba vba

基本上我有一个userform,我想用它将2个数据输入另一个我已经拥有的宏。用户表单如下:

enter image description here

基本上,我想点击OK按钮,两个框中的数据将被输入到我拥有的另一个宏中。如果确定按钮可以提供帮助,如果其中一个框未填满则会发出警告,这也会很棒。

到目前为止,我没有太多的代码......

Private Sub UserForm_Click()

TextBox1.SetFocus

Sub Enterval()


End Sub


Private Sub TextBox1_Change()
Dim ID As String

    ID = UserForm3.TextBox1.Value
End Sub

Private Sub TextBox2_Change()
Dim ID2 As String

    ID2 = UserForm3.TextBox2.Value
End Sub

Private Sub OKay_Click()

Enterval

End Sub

非常感谢任何提示和帮助。谢谢!

我的另一个宏

Private Sub CommandButton1_Click()

Dim Name As String
Dim Problem As Integer
Dim Source As Worksheet, Target As Worksheet
Dim ItsAMatch As Boolean
Dim i As Integer

Set Source = ThisWorkbook.Worksheets("Sheet1")
Set Target = ThisWorkbook.Worksheets("Sheet2")
Name = Source.Range("A3")
Problem = Source.Range("I13")

Do Until IsEmpty(Target.Cells(4 + i, 6)) ' This will loop down through non empty cells from row 5 of column 2
    If Target.Cells(4 + i, 6) = Name Then
        ItsAMatch = True
        Target.Cells(4 + i, 7) = Problem ' This will overwrite your "Problem" value if the name was already in the column
        Exit Do
    End If
    i = i + 1
Loop

' This will write new records if the name hasn't been already found
If ItsAMatch = False Then
    Target.Cells(3, 6).End(xlDown).Offset(1, 0) = Name
    Target.Cells(4, 6).End(xlDown).Offset(0, 1) = Problem
End If

Set Source = Nothing
Set Target = Nothing

End Sub

这就是我拥有的宏。正如你所说,我改变了

othermacro

CommandButton1_Click()

但它不起作用

2 个答案:

答案 0 :(得分:0)

不需要Enterval功能。相反,假设用户可以阅读并遵循说明,然后测试是否确实如此。请注意,在您的代码中IDID2将永远不会被使用,因为它们仅存在于声明它们并接收值的子例程的范围内。

开始使用:

    Sub Okay_Click()
        Dim sID1 As String, sID2 As String
        sID1 = UserForm3.TextBox1.Value
        sID2 = UserForm3.TextBox2.Value

        If Len(sID1 & vbNullString) = 0 Then
            MsgBox "Box A is empty"
            Exit Sub
        End If

        If Len(sID2 & vbNullString) = 0 Then
            MsgBox "Box B is empty"
            Exit Sub
        End If

'Now do something with sID1, sID2
        otherMacro(sID1, sID2)

    End Sub

对于你的其他宏,声明如下:

Sub otherMacro(ID1, ID2)
...
End Sub

此外,SetFocus方法应该以open事件的形式出现。

答案 1 :(得分:0)

引用geoB除了一件事:当你从主Sub .Show你的UserForm时,你也可以在最后.Hide,并且调用它的宏将继续其程序。

    Sub Okay_Click()
    Dim sID1 As String, sID2 As String

    ' A little variation
    If Me.TextBox1 = "" Or Me.TextBox2 = "" Then 
        MsgBox "Please fill all the input fields"
        Exit Sub
    End If

    Me.Hide

    End Sub

要解决TextBox问题,您可以在主要的子UserForm3.TextBox1中写一下