【问题标题】:Return Cell Address/Row After Countif返回单元格地址/计数后的行
【发布时间】:2017-06-11 13:09:00
【问题描述】:

一旦满足 countif 条件,我想寻求有关获取单元格地址或行的帮助。我觉得我只是缺少一些简单的东西。

这是我目前所拥有的大部分内容:

    Wb2.Activate
    Wb2.Sheets.Add(After:=Wb2.Sheets(Wb2.Sheets.Count)).Name = "Report"
    With Wb2.Sheets("From")
        Set myRange = .Range("F2:G" & lastRow)
    End With

    With Wb2.Sheets("Weekly_Stat")
        Set forComp = Wb2.Sheets("Weekly_Stat").Range("D2:E" & lastRow2)
    End With

    Sheets("From").Activate

    For Each item In myRange.Rows
        With item 
            .Select
            myCount = item.Row
            myBool = False
            foundIt = Application.CountIfs(forComp.Columns(1), .Cells(1).Value, forComp.Columns(2), .Cells(2).Value)
            If foundIt Then myBool = True
                If myBool = True Then
                    myStr = forComp.Row
                    Wb2.Sheets("Report").Cells(myCount, 10).Value = "Found"
                    MsgBox myStr
                Else
                    Wb2.Sheets("Report").Cells(myCount, 10).Value = "Not Found"
                End If
        End With
    Next item 

在代码中,我试图获取在 forComp 中找到的项目行,但我失败得很惨。

我想知道如何获得在“Weekly_Stat”中找到的匹配行号。

目前,我只能确定“来自”表中的数据是否在“Weekly_Stat”中找到。我在新的“报告”表中放置了“找到”和“未找到”列。

我想做的是:

  • 检查是否在 Weekly_Stat 中找到数据,将结果放入报告中(完成)
  • 发现Weekly_Stat中有数据后,需要获取地址/行
  • 我将使用行/地址从 Weekly_Stat 的同一行中的下一列中获取另一个数据。

我尝试搜索我的困境,但找不到解决方案。我可能使用了错误的关键字进行搜索,所以如果已经被问到,我提前道歉。任何帮助将不胜感激。非常感谢。

【问题讨论】:

  • 看起来您想返回两列匹配的行号。有更简单的方法可以做到这一点。

标签: excel vba cell


【解决方案1】:

很难确切地看到您要执行的操作,但似乎您想获取一张纸的列中的每个值,并查看该值是否存在于另一张纸的列中。如果这是正确的,那么我不相信你的方法是最好的方法。

通常,我们会迭代一列,然后再迭代一秒钟以测试每个匹配项,这将为我们提供找到的行的索引。是的,以Range 作为参数的 Excel 函数确实会运行内部迭代,但很难以这种方式控制(和调试)您的搜索,尤其是在您刚开始使用 VBA 时。

将单元格值读入数组并循环遍历它们要快得多(在我看来,更容易)。如果您有兴趣采用这条路线,那么您的代码可能类似于下面的骨架代码。注意:您需要调整所有范围定义和限定符以适应您自己的工作簿、工作表和需求。

还有更快但更复杂的方法来完成您的任务,尤其是在数据独特的情况下,但这提供了一些简单的数组循环来帮助您完成任务。

Dim fromVals As Variant, weekVals As Variant
Dim reportVals() As Variant
Dim r1 As Long, r2 As Long
Dim found As Boolean

'Read ranges into arrays for the two sheets.
'Note: define your ranges to suit.
With ThisWorkbook.Worksheets("From")
    fromVals = .Range(.Cells(2, "F"), _
               .Cells(.Rows.Count, "F").End(xlUp)) _
               .Resize(, 2).Value2
End With

With ThisWorkbook.Worksheets("Weekly_Stat")
    weekVals = .Range(.Cells(2, "D"), _
               .Cells(.Rows.Count, "D").End(xlUp)) _
               .Resize(, 2).Value2
End With

'Dimension the output array.
'Note: example uses num of rows in "From" sheet and 2 columns.
ReDim reportVals(1 To UBound(fromVals, 1), 1 To 2)

'Loop through "From array2 to acquire each value.
For r1 = 1 To UBound(fromVals, 1)
    found = False 'sets the found flag each iteration
    'Loop through "Week" array to look for match with "From" value.
    For r2 = 1 To UBound(weekVals, 1)
        If weekVals(r2, 1) = fromVals(r1, 1) Then
            'We've found a match
            reportVals(r1, 1) = "Found" 'writes found in same row index as "From" array
            reportVals(r1, 2) = weekVals(r2, 2) 'writes value from "G" column of "week" array
            found = True
            Exit For
        End If
    Next
    If Not found Then reportVals(r1, 1) = "Not found"
Next

'Writes report array to sheet.
ThisWorkbook.Worksheets("Report").Cells(2, 10) _
    .Resize(UBound(reportVals, 1), UBound(reportVals, 2)) _
    .Value = reportVals

【讨论】:

  • 非常感谢您提供非常清晰的解释。您是正确的,很难通过 excel 函数来操作搜索。我喜欢您使用可变数组进行搜索的想法。这真的很有帮助。非常感谢!
【解决方案2】:

如果我们要使用公式找到匹配的行,我们会在报告的第 2 行写下这个数组公式:

=MATCH(1,(Weekly_Stat!D:D=From!F2)*(Weekly_Stat!E:E=From!G2),0)  ' Ctrl+Shift+Enter

然后我们填写该列。要使用 VBA 自动执行该过程,我们可以这样做(替换从第一行到最后一行的所有代码,仅假设 lastRow 计算正确):

 With Wb2.Sheets.Add(After:=Wb2.Sheets(Wb2.Sheets.count))
   .name = "Report"
   .Range("J2").FormulaArray = _
      "=MATCH(1,(Weekly_Stat!D:D=From!F2)*(Weekly_Stat!E:E=From!G2),0)"
   .Range("J2:J" & lastrow).FillDown
 End With

【讨论】:

  • 非常感谢您的建议。代码简洁明了。我很高兴了解 .FillDown。非常感谢您的帮助!
猜你喜欢
  • 2023-03-24
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多