简体   繁体   English

Excel VBA UserForm-如果不是IsEmpty

[英]Excel VBA UserForm- If Not IsEmpty Then

I am making a userform that asks for input from the user. 我正在制作一个用户表单,要求用户输入。

There are lots of inputs, but I am having problems with one section in particular. 有很多输入,但是我在一节中特别遇到问题。 If a user enters ANYTHING into the textbox (named SecondCompetitor), I want the function to place some values into one of my excel worksheets (named ws4). 如果用户在文本框(名为SecondCompetitor)中输入了任何内容,我希望该函数将一些值放入我的excel工作表之一(名为ws4)中。

Here is the code I have written: 这是我编写的代码:

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 

When I actually execute the code, the values are placed in the rows REGARDLESS of whether or not a user has actually inputted anything into the SecondCompetitor textbox. 当我实际执行代码时,将值放置在REGARDLESS行中,该行与用户是否实际在SecondCompetitor文本框中输入了任何内容有关。

Any suggestions on what the problem may be? 关于问题可能有什么建议?

IsEmpty is used for arrays. IsEmpty用于数组。 use the below: 使用以下内容:

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 

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

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