Excel VBA UserForm-如果不是IsEmpty那么

时间:2016-07-13 13:43:01

标签: excel vba excel-vba if-statement userform

我正在制作一个要求用户输入的用户表单。

有很多输入,但我特别关注一个部分。如果用户在文本框中输入ANYTHING(名为SecondCompetitor),我希望该函数将一些值放入我的一个excel工作表(名为ws4)中。

这是我写的代码:

With ws3
     If Not IsEmpty(Me.SecondCompetitor.Value) Then
        .Cells(iRow3, 14).Value = Me.Ticker.Value
        .Cells(iRow3, 2).Value = Me.Rec1.Value
        .Cells(iRow3, 3).Value = Me.Rec2.Value
        .Cells(iRow3, 4).Value = Me.Rec3.Value
        .Cells(iRow3, 5).Value = Me.Rec4.Value
        .Cells(iRow3, 6).Value = Me.Rec5.Value
        .Cells(iRow3, 7).Value = Me.Rec6.Value
        .Cells(iRow3, 8).Value = Me.Rec7.Value
        .Cells(iRow3, 9).Value = Me.Rec8.Value
        .Cells(iRow3, 10).Value = Me.Rec9.Value
        .Cells(iRow3, 15).Value = Me.FirstCompetitor.Value
        .Cells(iRow3, 17).Value = Me.SecondCompetitor.Value
        .Cells(iRow3, 19).Value = Me.Winner.Value
        .Cells(iRow3, 20).Value = Me.Exploration.Value
        .Cells(iRow3, 21).Value = Me.DateAdded.Value
    End If
End With 

当我实际执行代码时,值将被放置在行中,无论用户是否实际在SecondCompetitor文本框中输入了任何内容。

有关问题的建议吗?

1 个答案:

答案 0 :(得分:1)

IsEmpty用于数组。使用以下内容:

With ws3
     If Not Me.SecondCompetitor.Value = "" Then 'This line could also be Me.SecondCompetitor.Value = vbNullString
        .Cells(iRow3, 14).Value = Me.Ticker.Value
        .Cells(iRow3, 2).Value = Me.Rec1.Value
        .Cells(iRow3, 3).Value = Me.Rec2.Value
        .Cells(iRow3, 4).Value = Me.Rec3.Value
        .Cells(iRow3, 5).Value = Me.Rec4.Value
        .Cells(iRow3, 6).Value = Me.Rec5.Value
        .Cells(iRow3, 7).Value = Me.Rec6.Value
        .Cells(iRow3, 8).Value = Me.Rec7.Value
        .Cells(iRow3, 9).Value = Me.Rec8.Value
        .Cells(iRow3, 10).Value = Me.Rec9.Value
        .Cells(iRow3, 15).Value = Me.FirstCompetitor.Value
        .Cells(iRow3, 17).Value = Me.SecondCompetitor.Value
        .Cells(iRow3, 19).Value = Me.Winner.Value
        .Cells(iRow3, 20).Value = Me.Exploration.Value
        .Cells(iRow3, 21).Value = Me.DateAdded.Value
    End If
End With