簡體   English   中英

循環遍歷 VBA 個變量

[英]Loop Through VBA Variables

我在 VBA 中創建了一個用戶表單,其中我有一個可以搜索項目的文本框。 我有大約 30 個文本框,所以我想使用循環來減少代碼,而不是復制和粘貼相同的代碼 30 次。

問題:我不知道如何遍歷公共變量

Public Variable: Public oEventHandler(Number) As New clsSearchableDropdown
     oEventHandler would go from 1 to 30 (e.g oEventHandler2,oEventHandler3...oEventHandler30)
     clsSearchableDropdown is the Class Module for the search feature

Text Box: TextBox(Number)
ListBox: ListBox(Number)

這是原始代碼(沒有問題只是為了比較)

  With oEventHandler1
                            
    ' Attach the textbox and listbox to the class
                                        
        Set .SearchListBox = Me.ListBox1
        Set .SearchTextBox = Me.TextBox1
                                        
    ' Default settings
                                    
        .MaxRows = 10
        .ShowAllMatches = True
        .CompareMethod = vbTextCompare
        .WindowsVersion = False
                            
  End With

這就是我想要做的:

  Dim i As Integer

  for i = 1 to 30

    With Me.Controls.Item("oEventHandler" & i)
                            
      ' Attach the textbox and listbox to the class
                                        
          Set .SearchListBox = Me.Controls.Item("ListBox" & i)
          Set .SearchTextBox = Me.Controls.Item("TextBox" & i)
                                        
      ' Default settings
                                    
          .MaxRows = 10
          .ShowAllMatches = True
          .CompareMethod = vbTextCompare
        .WindowsVersion = False
                              
    End With

  Next i

我知道 oEventHandler 不是控件,但是是否有類似的代碼可以用來循環訪問公共變量?

這是對我有用的代碼:

' Make a New Collection

  Dim coll As New Collection

' Add all Public Variables to Collection (n = Number 1 to 30)

  coll.Add uQuote.oEventHandler(n) 
     (e.g oEventHandler1, oEventHandler2... oEventHandler30)

  Dim i As Integer

  for i = 1 to 30

    With coll(i)
                            
      ' Attach the textbox and listbox to the class
                                        
          Set .SearchListBox = Me.Controls.Item("ListBox" & i)
          Set .SearchTextBox = Me.Controls.Item("TextBox" & i)
                                        
      ' Default settings
                                    
          .MaxRows = 10
          .ShowAllMatches = True
          .CompareMethod = vbTextCompare
        .WindowsVersion = False
                              
    End With

  Next i

如果我理解正確的話,在用戶窗體中你有 30 個文本框和 30 個列表框,其中每個文本框 (N) 用於搜索位於該文本框 (N) 下的列表框 (N) 中的值。 所以它看起來像這樣:
在此處輸入圖像描述

左邊是TextBox01,TextBox01下面是ListBox01
右邊是TextBox02,TextBox02下面是ListBox02

如果 animation 與您的期望相似......

准備:

  • 使用 List01、List02、List03 等內容創建一個命名范圍(根據需要盡可能多),以便填充每個 ListBox 的值。
  • 使用 ListBox01、ListBox02 等名稱命名每個 ListBox。
  • 使用 TextBox01、TextBox02 等名稱命名每個 TextBox。

在用戶表單模塊中:

Dim MyTextBoxes As Collection

Private Sub UserForm_Initialize()

'populate the ListBoxes with value in a named range
Dim LBname As String: Dim RGname As String: Dim i As Integer
For i = 1 To 2
LBname = "ListBox" & Format(i, "00")
RGname = "List" & Format(i, "00")
Controls(LBname).List = Application.Transpose(Range(RGname))
Next i

'add each TextBox to class
Set MyTextBoxes = New Collection
    For Each ctl In Me.Controls
        Set TextBoxClass = New Class1
        If TypeName(ctl) = "TextBox" And InStr(ctl.Name, "TextBox") Then Set TextBoxClass.obj = ctl
        MyTextBoxes.Add TextBoxClass
    Next
    
End Sub

在名為 Class1 的 Class 模塊中:

Private WithEvents tb As MSForms.TextBox

Property Set obj(t As MSForms.TextBox)
Set tb = t
End Property

Private Sub tb_Change()
Dim idx As String: Dim LBname As String: Dim arr
idx = Right(tb.Name, 2)
LBname = "ListBox" & idx
arr = Application.Transpose(Range("List" & idx))
    With Userform1.Controls(LBname)
        If tb.text = "" Then
            .Clear
            .List = arr
        Else
            .Clear
            For i = LBound(arr, 1) To UBound(arr, 1)
                If LCase(arr(i)) Like "*" & LCase(tb.value) & "*" Then .AddItem arr(i)
            Next i
        End If
    End With
End Sub

如果在您的用戶窗體中您有另一個文本框不用作搜索相應列表框中的項目,那么也許不要使用“TextBox”命名文本框,而是使用其他名稱,例如“blablabla”。

如果您現有的文本框和列表框已經命名為 ListBox1、ListBox2、ListBox3 等,TextBox1、TextBox2、TextBox3 等...然后命名命名范圍,如 List1、List2、List3 等。 在 class 模塊中,使用 replace 方法更改 idx 的代碼,類似於idx = replace(tb.name,"TextBox","") 同樣在 LBname 和 RGname 的用戶窗體模塊中使用 replace 方法。

由於我的英語水平有限,很抱歉我無法詳細說明代碼以供進一步解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM