gpt4 book ai didi

excel - 如何检查列表框的一行是否为空

转载 作者:行者123 更新时间:2023-12-04 20:26:17 25 4
gpt4 key购买 nike

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

到目前为止我的代码:

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

最佳答案

determine if the first column along the row of a listbox is empty.



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

这是你正在尝试的吗?
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

enter image description here

顺便说一句,你有你的 End WithEnd If 之前.也许是错字?

注意 : 我正在考虑将第二列作为第一列。如果您的意思是第一列中的第一列,那么而不是 1使用 0 .例如 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

enter image description here

关于excel - 如何检查列表框的一行是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59738464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com