我想在获得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"中找到的匹配的行号。
目前,我只能确定来自" From"的数据。表格在" Weekly_Stat"中找到。我为"找到了#34;和#34;未找到"在一个新的"报告"片材。
我想做的是:
我试图寻找我的困境,但我无法找到解决方案。我可能会使用错误的关键字进行搜索,所以如果已经提出要求,我会提前道歉。任何帮助将不胜感激。非常感谢。
答案 0 :(得分:0)
很难确切地看到您正在尝试做什么,但似乎您希望将每个值都放在一个工作表的列中,并查看该值是否存在于另一个工作表的列中片。如果这是正确的,那么我不相信你的方法是最好的方法。
通常,我们会迭代一列然后迭代一秒来测试每个matche,这将为我们提供找到的行的索引。是的,将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
答案 1 :(得分:0)
如果我们要使用公式找到匹配的行,我们会在报告的第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