【问题标题】:Excel VBA Find.Method , searching for 2 Values in a row writing a valueExcel VBA Find.Method ,连续搜索2个值写入一个值
【发布时间】:2015-09-30 14:51:35
【问题描述】:

我想在表格中搜索,例如

A B C
D E C
嘿嘿
A H C
对于价值观

“A”和“C”,然后将值“Value”放入已找到这两个值的行中的单元格中。
我对整个主题相当陌生,所以我首先在网上搜索以找到可以帮助我的代码片段。

Dim FirstAddress As String
Dim SecondAddress As String
Dim MyArr As Variant
Dim MyArr2 As Variant
Dim Rng As Range
Dim Row As Range
Dim I As Long
Dim B As Long
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
MyArr = Array("A")
MyArr2 = Array("C")
With Sheets("Sheet1").Range("F:F")
    .Offset(0, 27).ClearContents
    For I = LBound(MyArr) To UBound(MyArr)
        Set Rng = .Find(What:=MyArr(I), After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

        If Not Rng Is Nothing Then
            FirstAddress = Rng.Address
            Do
                    Set Rng2 = Rng.EntireRow.Find(What:=MyArr2(I), After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                    If Not Row Is Nothing Then
                        SecondAdress = Row.Address
                        Do
                            Rng.Offset(0, 27).Value = "Value"
                            Set Row = .FindNext(Row)

                Set Rng = .FindNext(Rng)
            Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
        End If
    Next I
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

它对我使用 .find 方法搜索一个值很有用,但我很难搜索包含这两个值的行。 (如果我想搜索多个值的数组,比如说所有在开头的 A 或 D,然后是第三列中的 C)

你有什么想法吗? 我不明白如何实现几个循环。

谢谢!

【问题讨论】:

标签: excel find vba


【解决方案1】:

从以下数据开始:

运行这个宏:

Sub dural()
   Dim N As Long, i As Long, A As String, C As String, v As String
   Dim rng1 As Range, rng2 As Range, wf As WorksheetFunction
   Set wf = Application.WorksheetFunction
   A = "A"
   C = "C"
   v = "VALUE"
   N = Cells(Rows.Count, A).End(xlUp).Row
   Set rng1 = Range("A1:C" & N)

   For i = 1 To N
      Set rng2 = Intersect(Rows(i), rng1)
      If wf.CountIf(rng2, A) > 0 And wf.CountIf(rng2, C) > 0 Then
         Cells(i, "D") = v
      End If
   Next i
End Sub

将产生:

【讨论】:

  • 嗨,谢谢,不幸的是它在这里告诉我 N = Cells(Rows.Count, A).End(xlUp).Row 是一个问题,运行时错误 1004“应用程序定义或对象定义错误”
  • @JohnDoe 如果列 A 为空,则会发生这种情况。在我的示例中,我使用列 A,您使用的是哪些列??
  • 嗨,我实际上使用了 F 和 G 列,从 F1 填充到 G11,并修改了代码。 Private Sub ToggleButton1_Click() If ToggleButton1.Value = True Then Dim num As Long, row As Long, search1 As String, search2 As String, result As String Dim rotalrng As Range, currentrng As Range, count As WorksheetFunction Set count = Application.WorksheetFunction search1 = "a" search2 = "b" result = "Yes" num = Cells(Rows.count, F).End(xlUp).row Set totalrng = Range("F1:G" & num) For row = 1 To num Set currentrng = Intersect(Rows(row), totalrng)
  • If count.CountIf(currentrng, search1) > 0 and count.CountIf(currentrng, search2) > 0 Then Cells(row, "H") = result End If Next row End If End If End Sub
【解决方案2】:

高级过滤可帮助您识别符合条件的行。

数据 > 排序和筛选 > 高级

Microsoft Support - Filter by using advanced criteria

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多