我如何检查列表框的行是否为空

我如何检查列表框的行是否为空

本文介绍了我如何检查列表框的行是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VBA中编写IF语句,以确定列表框行中的第一列是否为空.如果该行为空,我希望显示一个消息框.如果该行不为空,则该行将保存到内存中.

I'm trying to write an IF statement in VBA to determine if the first column along the row of a listbox is empty.If the row is empty I want a message box to display. if the row is not empty then the row is saved to memory.

到目前为止,我的代码:

My code so far:

Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
   With RiskLogReviewListBox
      If IsNull(Me.RiskLogReviewListBox, 0, 0) Then
         MsgBox "Item is not a Valid Entry"
      Else
         str1$ = .List(.ListIndex, 0)
         str2$ = .List(.ListIndex, 1)
         str3$ = .List(.ListIndex, 2)
         str4$ = .List(.ListIndex, 3)
         str5$ = .List(.ListIndex, 4)
         str6$ = .List(.ListIndex, 5)
         str7$ = .List(.ListIndex, 6)
         str8$ = .List(.ListIndex, 7)
         str9$ = .List(.ListIndex, 8)
         str10$ = .List(.ListIndex, 9)
   End With
   End If

   Unload Me

   RiskRecordEditForm.Show
End Sub

推荐答案

您可以使用Len()Trim()来检查列表框行中的第一列是否为空.

You can use Len() and Trim() to check if the first column along the row of a listbox is empty.

这是您要尝试的吗?

Option Explicit

'~~> Add sample data
Private Sub CommandButton1_Click()
    With RiskLogReviewListBox
        .AddItem
        .List(UBound(.List), 0) = "aa"
        .List(UBound(.List), 1) = "bb"

        .AddItem
        .List(UBound(.List), 0) = "cc"
        .List(UBound(.List), 1) = "" '<~~ Empty
    End With
End Sub

Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    If RiskLogReviewListBox.ListIndex = -1 Then Exit Sub

    If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 1))) = 0 Then _
    MsgBox "First Column of selected row is empty"
End Sub

顺便说一句,您在End If之前有End With.也许是错字?

BTW you have your End With before the End If. Maybe a typo?

注意:我正在考虑将第二列作为第一列.如果您的意思是第一列与第一列相同,请使用0代替1.例如RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0)

NOTE: I am considering the second column as the first column. If you meant the first column as in the first column then instead of 1 use 0. For example RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0)

Option Explicit

'~~> Add sample data
Private Sub CommandButton1_Click()
    With RiskLogReviewListBox
        .AddItem
        .List(UBound(.List), 0) = "aa"
        .List(UBound(.List), 1) = "bb"

        .AddItem
        .List(UBound(.List), 0) = "" '<~~ Empty
        .List(UBound(.List), 1) = "cc"
    End With
End Sub

Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    If RiskLogReviewListBox.ListIndex = -1 Then Exit Sub

    If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
    MsgBox "First Column of selected row is empty"
End Sub

这篇关于我如何检查列表框的行是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:20