2017-06-11 59 views
0

我希望在符合countif條件時獲取有關獲取單元地址或行的幫助。我覺得我只是想念一些簡單的東西。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存在數據後,我需要得到的地址/ row
  • 我將使用行/地址從Weekly_Stat的同一行中的下一列中獲取另一個數據。

我試圖尋找我的困境,但我無法找到解決方案。我可能會使用錯誤的關鍵字進行搜索,所以如果已經提出了這個問題,我很抱歉。任何幫助將不勝感激。非常感謝。

+0

它看起來像你想返回的行數字爲兩列匹配。有更簡單的方法來做到這一點。 – Jeeped

回答

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 
+0

非常感謝您提供了非常明確的解釋。你是正確的,很難通過Excel函數操縱搜索。我喜歡使用變量數組進行搜索的想法。這真的很有幫助。非常感謝! – Pinked

0

如果我們發現使用公式匹配的行,我們將在報告的第二行寫這個數組公式:

=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 
+0

非常感謝您的建議。代碼簡潔明瞭。我很高興瞭解.FillDown。非常感謝您的幫助! – Pinked