【问题标题】:How to loop through all columns using VBA in Excel如何在 Excel 中使用 VBA 遍历所有列
【发布时间】:2021-05-23 12:18:09
【问题描述】:

根据教程,我在 Excel 中创建了一个小型联系人管理器,并针对我自己的目的进行了一些调整。到目前为止,作为一个 VBA 菜鸟,对我来说是一个很好的小经历:)

一些背景信息

我有两张床单。第一个包含人员及其地址。第二个包含他们所有的联系人详细信息(以防止第一张纸上有无限的列用于不同的电话、邮件等)。详细信息根据第一张表中数据的 ID 进行匹配,并显示在两个列表框中。 搜索值存储在 C5 中。 C4 对特定类型数据(如姓名、地址、地点)的列的引用,当我要搜索所有列时为空。

问题

当我尝试搜索某些内容时,它只会返回找到的第一个项目并停止。我想我需要创建一个循环来获取所有项目,但到目前为止我还没有成功创建一个正常运行的循环。

我目前拥有的代码

Private Sub btnZoeken_Click()
'dim the variables
Dim Crit As Range
Dim FindMe As Range
Dim DataSH As Worksheet

On Error GoTo errHandler:

Set DataSH = Sheet1

Application.ScreenUpdating = False

'Default search criteria is Alles (all columns).
If Me.cboHeader.Value <> "Alles" Then
        If Me.txtZoeken = "" Then
        DataSH.Range("C5") = ""
        Else
        DataSH.Range("C5") = "*" & Me.txtZoeken.Value & "*"
        End If
End If

'if all columns is selected
If Me.cboHeader.Value = "Alles" Then
'find the value in the column
    Set FindMe = DataSH.Range("B9:H30000").Find(What:=txtZoeken, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
'variable for criteria header
    Set Crit = DataSH.Cells(8, FindMe.Column)
'if no criteria is added to the search
        If Me.txtZoeken = "" Then
        DataSH.Range("C5") = ""
        DataSH.Range("C4") = ""
        Else
        'add values from the search
        DataSH.Range("C4") = Crit
            If Crit = "ID" Then
            DataSH.Range("C5") = Me.txtZoeken.Value
            Else
            DataSH.Range("C5") = "*" & Me.txtZoeken.Value & "*"
            End If
        End If
End If


'filter the data
DataSH.Range("B8").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("Data!$C$4:$C$5"), CopyToRange:=Range("Data!$N$8:$T$8"), _
Unique:=False
'add the dynamic data to the listbox
lstResult.RowSource = DataSH.Range("outdata").Address(external:=True)

'show which column contained to selected value (for now only for debugging)
Me.RegTreffer.Value = DataSH.Range("C4")

'error handler
On Error GoTo 0
Exit Sub
errHandler:
'if error occurs then show me exactly where the error occurs
MsgBox "No result for " & txtZoeken.Text & " in " & Me.cboHeader.Value
'clear the listbox if no match is found
Me.lstResult.RowSource = ""
Exit Sub
End Sub

我应该如何构建一个循环来获取任何列中具有匹配值的所有行?我需要创建两个不同的循环吗?一种用于在所有列中搜索,另一种用于搜索特定列,还是没关系?

【问题讨论】:

    标签: excel vba loops


    【解决方案1】:

    首先,直接回答您的问题:

    Private Sub LookForMatches()
    
        ' Set a reference to our range
        Dim MyRange As Range
        Set MyRange = ThisWorkbook.Sheets("Sheet1").Range("A1:C4")
        
        ' Loop through all of the cells in the range
        Dim Cell As Variant
        For Each Cell In MyRange.Cells
        
            ' In this example, we will check if the cell equals 1
            ' If it does, display a message informing the user of the match and the location of the cell
            If Cell.Value = 1 Then
                MsgBox "Match found.  The cell " & Cell.Address & " equals 1."
            End If
        
        Next Cell
        
    End Sub
    

    这是用于我的示例的 Sheet1 上的数据。

    希望这个模板足以说明如何遍历范围并查找信息。如果您只对单元格的行而不是完整地址感兴趣,您可以使用Cell.Row 方法而不是Cell.Address 方法。

    其次,您通常不应该循环访问这样的数据。它比其他方法慢得多。例如,我们可以将此范围存储在一个数组中,然后使用该数组而不是该范围。但这完全超出了您的问题范围。我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2010-11-10
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多