Excel 用户表单列表框空行

问题描述 投票:0回答:0

我正在制作一个带有列表框和删除按钮的用户表单。列表框从表中获取数据并将其显示到列表框行中。单击删除时,我添加了一个函数,如果没有从列表框中选择任何行,则告诉用户选择一行。我试图添加一个函数来告诉用户如果他们选择一个空行就没有什么可以删除的。它didn't work as intended and just continued on with the script.

这是脚本的开头,如果我需要添加脚本的其余部分,请告诉我。

Private Sub Delete_Click()
    If Not ListBox1.Selected(ListBox1.ListIndex) Then
        MsgBox "Please select a row to delete"
        Exit Sub
    End If
    
    If Len(Trim(ListBox1.List(ListBox1.ListIndex, 0))) = 0 Then
        MsgBox "Nothing to delete here"
        Exit Sub
    End If
    
    Dim response As VbMsgBoxResult
    response = MsgBox("Are you sure you want to delete the selected rows?", vbYesNo, "You sure?")
    
    'If no then do nothing
    If response = vbNo Then
        Exit Sub
    Else

我也尝试过这些但没有运气

If ListBox1.ListIndex > -1 And Len(ListBox1.List(ListBox1.ListIndex)) = 0 Then
    MsgBox "Nothing to delete here"
    Exit Sub
End If
If ListBox1.ListIndex > -1 And ListBox1.ListCount > 0 And Trim(ListBox1.List(ListBox1.ListIndex)) = "" Then
    MsgBox "Nothing to delete here"
    Exit Sub
End If
excel vba listbox userform
© www.soinside.com 2019 - 2024. All rights reserved.