如果组合框等于空,请不要将用户表单数据复制到Excel工作表

时间:2015-10-28 17:36:17

标签: excel vba excel-vba

我创建了2个这样的子功能:

Sub Product1()

Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws

If IsEmpty(UserForm5.ComboBox5.Value) Then
    Exit Sub
Else
    .Cells(lRow, 1).Value = UserForm5.TextBox1.Value
    .Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
    .Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
    .Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
    .Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
    .Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
    .Cells(lRow, 7).Value = UserForm5.TextBox2.Value
    .Cells(lRow, 8).Value = UserForm5.TextBox5.Value
    .Cells(lRow, 9).Value = UserForm5.TextBox6.Value
    .Cells(lRow, 10).Value = UserForm5.TextBox4.Value
    .Cells(lRow, 11).Value = UserForm5.TextBox7.Value

End If

End With

End Sub


Sub Product2()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws

If IsEmpty(UserForm5.ComboBox6.Value) Then
    Exit Sub
Else
    .Cells(lRow, 1).Value = UserForm5.TextBox1.Value
    .Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
    .Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
    .Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
    .Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
    .Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
    .Cells(lRow, 7).Value = UserForm5.TextBox9.Value
    .Cells(lRow, 8).Value = UserForm5.TextBox11.Value
    .Cells(lRow, 9).Value = UserForm5.TextBox12.Value
    .Cells(lRow, 10).Value = UserForm5.TextBox10.Value
    .Cells(lRow, 11).Value = UserForm5.TextBox8.Value
End If

End With

End Sub

我想知道,如果我的combobox6是空的,它不应该将数据传输到exel表。

我现在面临的问题是如果组合框6是空的(没有选择任何值),它仍然会将所有数据复制到Excel工作表。

有没有办法解决它?

2 个答案:

答案 0 :(得分:0)

更改

If IsEmpty(UserForm5.ComboBox5.Value) Then

If UserForm5.ComboBox5.Value = "" Then

对Product2子进行同样的更改。

答案 1 :(得分:0)

如果组合框是"空"然后检查它的值会给你一个空字符串。

Sub Product1()
    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Inventory")
    lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    With ws
        If UserForm5.ComboBox5.Value <> "" Then
            Exit Sub
        Else
            .Cells(lRow, 1).Value = UserForm5.TextBox1.Value
            .Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
            .Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
            .Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
            .Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
            .Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
            .Cells(lRow, 7).Value = UserForm5.TextBox2.Value
            .Cells(lRow, 8).Value = UserForm5.TextBox5.Value
            .Cells(lRow, 9).Value = UserForm5.TextBox6.Value
            .Cells(lRow, 10).Value = UserForm5.TextBox4.Value
            .Cells(lRow, 11).Value = UserForm5.TextBox7.Value
        End If
    End With
End Sub

Sub Product2()
    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Inventory")
    lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    With ws
        If UserForm5.ComboBox6.Value <> "" Then
            Exit Sub
        Else
            .Cells(lRow, 1).Value = UserForm5.TextBox1.Value
            .Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
            .Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
            .Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
            .Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
            .Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
            .Cells(lRow, 7).Value = UserForm5.TextBox9.Value
            .Cells(lRow, 8).Value = UserForm5.TextBox11.Value
            .Cells(lRow, 9).Value = UserForm5.TextBox12.Value
            .Cells(lRow, 10).Value = UserForm5.TextBox10.Value
            .Cells(lRow, 11).Value = UserForm5.TextBox8.Value
        End If
    End With
End Sub
相关问题