按下命令按钮后,Userform不会卸载

时间:2015-06-22 04:22:16

标签: vba excel-vba excel

我的用户窗体有点麻烦,一旦我点击命令按钮就没有卸载,数据被输入到工作表但是用户窗体没有刷新,数据保留在文本框中。它工作正常,直到我把数据验证,但我不能删除数据验证,因为它是关键在那里,任何建议我需要寻找什么?

Private Sub CommandButton1_Click()
Dim emptyRow As Long

If Not IsNumeric(TextBox1.Value) Then
MsgBox ("Sorry, you need to provide a valid order number")
TextBox1.SetFocus
Exit Sub
End If

If TextBox3.Value = "" Then
MsgBox ("Sorry, you need to provide a weight")
TextBox3.SetFocus
Exit Sub
End If

If TextBox4.Value = "" Then
MsgBox ("Sorry, you need to provide a country code")
TextBox4.SetFocus
Exit Sub
End If

If TextBox2.Value = "" Then
MsgBox ("Sorry, you need to provide a country")
TextBox2.SetFocus
Exit Sub
End If

If ComboBox1.Value = "" Then
MsgBox ("Sorry, you need to provide a service")
ComboBox1.SetFocus
Exit Sub
End If

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
If ComboBox1 = "EU" Then
Cells(emptyRow, 1).Value = TextBox1.Value
Cells(emptyRow, 4).Value = UCase(TextBox4.Value)
Cells(emptyRow, 5).Value = UCase(TextBox2.Value)
Cells(emptyRow, 2).Value = TextBox3.Value
Cells(emptyRow, 6).Value = ComboBox1.Value
Else
If ComboBox1 = "ROW" Then
Cells(emptyRow, 1).Value = TextBox1.Value
Cells(emptyRow, 4).Value = UCase(TextBox4.Value)
Cells(emptyRow, 5).Value = UCase(TextBox2.Value)
Cells(emptyRow, 3).Value = TextBox3.Value
Cells(emptyRow, 6).Value = ComboBox1.Value
Else

End Sub

Cells(emptyRow, 7).Value = Date

Unload Me
Application.ActiveWorkbook.Save
ParcelDataEntry.Show
End If
End Sub

1 个答案:

答案 0 :(得分:1)

您发布的代码存在一些问题。

If ComboBox1 = "ROW" Then ... Else代码块之后,您有End Sub但没有End If。您肯定需要添加End If,我怀疑您应该删除End Sub

在最后一个If ... End If块中有三行代码,我怀疑只要用户正确完成表单上的控件就要运行。这些应移到If ... End If块之外。

您正在重复代码块,用于测试用户是否已在控件中输入值。这些应该被提取到一个函数中。您还在重复写入工作表的代码行。这些可以移到If ... End If块之外,但留下不同的线。

我认为这就是您希望代码看起来的样子:

Private Sub CommandButton1_Click()
Dim emptyRow As Long

    If Not IsNumeric(TextBox1.Value) Then
        MsgBox ("Sorry, you need to provide a valid order number")
        TextBox1.SetFocus
        Exit Sub
    End If

    If Not UserEnteredAValue(TextBox3, "weight") Then
        Exit Sub
    End If

    If Not UserEnteredAValue(TextBox4, "country code") Then
        Exit Sub
    End If

    If Not UserEnteredAValue(TextBox2, "country") Then
        Exit Sub
    End If

    If Not UserEnteredAValue(ComboBox1, "service") Then
        Exit Sub
    End If

    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

    'Transfer information
    ' These cells always get the same value
    Cells(emptyRow, 1).Value = TextBox1.Value
    Cells(emptyRow, 4).Value = UCase(TextBox4.Value)
    Cells(emptyRow, 5).Value = UCase(TextBox2.Value)
    Cells(emptyRow, 6).Value = ComboBox1.Value
    Cells(emptyRow, 7).Value = Date

    If ComboBox1 = "EU" Then
        Cells(emptyRow, 2).Value = TextBox3.Value
    Else
        If ComboBox1 = "ROW" Then
            Cells(emptyRow, 3).Value = TextBox3.Value
        Else

        End If

    End If

    Unload Me
    Application.ActiveWorkbook.Save
    ParcelDataEntry.Show

End Sub

Private Function UserEnteredAValue(ByRef theControl As Control, ByRef theDescription As String) As Boolean

Dim result As Boolean

    If theControl.Value <> "" Then
        result = True
    Else
        MsgBox ("Sorry, you need to provide a " & theDescription)
        theControl.SetFocus
        result = False
    End If

    UserEnteredAValue = result

End Function

PS当您使用CellsRange方法时,您应该始终使用工作簿和工作表参考来限定它,例如: Workbooks("book_name.xlsm").Worksheets("sheet_name").Cells(emptyRow, 1)以便您确切地知道工作簿&amp;你正在使用的工作表。