VBA用户窗体列表框条件逻辑无法按预期工作

时间:2018-12-20 13:40:42

标签: excel vba listbox userform

我有一个带有列表框的用户窗体,我正在使用条件逻辑来确定到该列表框中选定或未选定项目表的输出值。问题是,当文本框(Tbl_AliasName)为空白时,代码将执行以下操作:

ElseIf .Selected(k) = True And Tbl_AliasName = vbNullString Then
            lstbxRow = lstbxRow + 1

                Worksheets("New TRAX").Cells(lstbxRow, 3) = Trim(Cells(2, 1).Value2) & "." & .Column(1, k)

但是如果Tbl_AliasName不为空,则代码不执行任何操作,但应该这样做:

 ElseIf .Selected(k) = True And Tbl_AliasName <> vbNullString Then
            lstbxRow = lstbxRow + 1

                Worksheets("New TRAX").Cells(lstbxRow, 3) = Tbl_AliasName & "." & .Column(1, k)

我使用了If语句的几种变体,但没有一种奏效。
以下是我的代码:

Option Explicit
Public Tbl_AliasName As String
Tbl_AliasName = Trim(UserForm_Finder.txtConcat.Value)

Private Sub BtnConcat_Click()
Dim k As Long, lstbxRow As Long, LR As Long

lstbxRow = 1

 '****************
'This if statement works perfectly
If (Cells(2, 1).Value2 = vbNullString Or Cells(2, 2).Value2 = vbNullString) _
        And Tbl_AliasName = vbNullString Then

        MsgBox "You must Search for a Table or Column first.", _
        vbExclamation, "Error Encountered"

        Exit Sub

ElseIf (UserForm_Finder.ListBx_TblsCols.ListCount = 0 And Tbl_AliasName <> vbNullString) Then
        MsgBox "You must Search for a Table or Column first.", _
        vbExclamation, "Error Encountered"
        '(Cells(2, 1).Value2 = vbNullString Or Cells(2, 2).Value2 = vbNullString) And _

        Exit Sub

End If


With UserForm_Finder.ListBx_TblsCols
    For k = 0 To .ListCount - 1
        '****************
         This is where the problems begin
    If .Selected(k) = False Then
            MsgBox "You must Select 1 or more items from the list box.", _
            vbExclamation, "Error Encountered"
            Exit Sub

        ElseIf .Selected(k) = True And Tbl_AliasName <> vbNullString Then
            lstbxRow = lstbxRow + 1

                Worksheets("New TRAX").Cells(lstbxRow, 3) = Tbl_AliasName & "." & .Column(1, k)

        ElseIf .Selected(k) = True And Tbl_AliasName = vbNullString Then
            lstbxRow = lstbxRow + 1

                Worksheets("New TRAX").Cells(lstbxRow, 3) = Trim(Cells(2, 1).Value2) & "." & .Column(1, k)

        End If
    Next k
  End With
End Sub

我的目标是执行以下操作:

  1. 如果文本框(Tbl_AliasName)不为空,并且用户已在列表框(ListBx_TbleCols)中选择了一个或多个项目,则将Tbl_AliasName连接到列表框中的选定项
  2. 如果Tbl_AliasName为空白,则使用Cells(2,1)中的值连接到列表框中的选定项。

我尝试了以下添加功能:

Dim LstBxItemSelected As Boolean

'This was placed in the for loop
LstBxItemSelected = True

'this was placed outside the for loop
If LstBxItemSelected = False Then
      MsgBox "You must Select 1 or more items from the list box.", _
             vbExclamation, "Error Encountered"
      Exit Sub       
End If

是否有更好的方法来判断是否选择了项目,因为我觉得在循环中将其结构化,如果未选择所有内容,代码将引发错误?预先感谢您的任何想法,答案或建议!

注意:列表框是通过在用户窗体上单击另一个按钮来填充的,该按钮将调用以下子项:

Sub FillLstBxCols()  

    Dim ListBx_Target As MSForms.ListBox
    Dim rngSource As Range
    Dim LR As Long

    If Cells(2, 1).Value2 <> vbNullString Then
        LR = Worksheets("New TRAX").Cells(Rows.Count, 2).End(xlUp).Row


        'Set reference to the range of data to be filled
        Set rngSource = Worksheets("New Trax").Range("A" & 2 & ":" & "B" & LR)

        'Fill the listbox
        Set ListBx_Target = UserForm_Finder.ListBx_TblsCols
        With ListBx_Target
            .RowSource = rngSource.Address
        End With
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

在没有样本数据和预期结果的情况下很难说,但是我认为这是您想要的:

Private Sub btnConcat_Click()

    Dim ws As Worksheet
    Dim bSelected As Boolean
    Dim sConcat As String
    Dim i As Long, lRowIndex As Long

    Set ws = ActiveWorkbook.Sheets("New TRAX")
    lRowIndex = 1
    bSelected = False
    sConcat = Trim(Me.txtConcat.Text)
    If Len(sConcat) = 0 Then sConcat = Trim(ws.Cells(2, "A").Value)
    If Len(sConcat) = 0 Then
        MsgBox "You must Search for a Table or Column first.", vbExclamation, "Error Encountered"
        Exit Sub
    End If

    For i = 0 To Me.ListBx_TblsCols.ListCount - 1
        If Me.ListBx_TblsCols.Selected(i) Then
            If bSelected = False Then
                bSelected = True
                ws.Range("C2", ws.Cells(ws.Rows.Count, "C")).Clear  'clear previous concat results (delete this line if not needed)
            End If
            lRowIndex = lRowIndex + 1
            ws.Cells(lRowIndex, "C").Value = sConcat & "." & Me.ListBx_TblsCols.List(i)
        End If
    Next i

    If bSelected = False Then MsgBox "Must select at least one item from the list"

End Sub