我正嘗試使用新票證數據更新當前工作票證數據的工作簿。我已經爲底部的每個循環嵌套,以檢查票號中的相等性。如果它找到匹配,它應該用新數據更新一些單元格。如果該票不在我的票單列表中,則應該將新票添加到底部。發生的事情是,即使是電子表格中的票據,它也會不斷將newData中的所有票據添加到currentData的底部。我認爲問題最終取決於這些嵌套for循環的邏輯,但我無法弄清楚我做錯了什麼。Excel VBA - 檢查一個範圍內的每個單元格的值是否在另一個範圍內
Sub getNewData()
Dim newData As Workbook
Dim ndLastRow As Long
Dim currentData As Workbook
Dim cdLastRow As Long
Dim ndRangeToCheck As Range
Dim cdRangeToCheck As Range
Dim ndRow As Long
Dim cdRow As Long
Set newData = Workbooks.Open("C:\Users\<user>\Documents\newData.xlsx")
Set currentData = ThisWorkbook
' Assign last row and the range to compare for each workbook
newData.Worksheets("Incident List").Range("A2").Select
With ActiveSheet
ndLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Set ndRangeToCheck = newData.Worksheets("Incident List").Range("A2", Cells(ndLastRow, "A"))
currentData.Worksheets("Incident List").Activate
With ActiveSheet
cdLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Set cdRangeToCheck = currentData.Worksheets("Incident List").Range("B2", Cells(cdLastRow, "B"))
' Iterate through to compare Incident #s between workbooks
Dim rout As Range
Dim rin As Range
Dim match As Boolean
For Each rout In ndRangeToCheck.Cells
match = False
For Each rin In cdRangeToCheck.Cells
If Cells(rin.Row, rin.Column).Value = Cells(rout.Row, rout.Column).Value Then
match = True
ndRow = rout.Row
cdRow = rin.Row
currentData.Worksheets("Incident List").Cells(cdRow, "L").Value = newData.Worksheets("Incident List").Cells(ndRow, "D").Value
currentData.Worksheets("Incident List").Cells(cdRow, "O").Value = newData.Worksheets("Incident List").Cells(ndRow, "F").Value
currentData.Worksheets("Incident List").Cells(cdRow, "P").Value = newData.Worksheets("Incident List").Cells(ndRow, "G").Value
currentData.Worksheets("Incident List").Cells(cdRow, "Q").Value = newData.Worksheets("Incident List").Cells(ndRow, "H").Value
currentData.Worksheets("Incident List").Cells(cdRow, "S").Value = newData.Worksheets("Incident List").Cells(ndRow, "L").Value
currentData.Worksheets("Incident List").Cells(cdRow, "T").Value = newData.Worksheets("Incident List").Cells(ndRow, "N").Value
currentData.Worksheets("Incident List").Rows(rin.Row).Borders.LineStyle = xlContinuous
Exit For
End If
Next rin
If match = False Then
ndRow = rout.Row
currentData.Worksheets("Incident List").Cells(cdLastRow, "B").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "A").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "B").Offset(1, 0).NumberFormat = "0"
currentData.Worksheets("Incident List").Cells(cdLastRow, "L").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "D").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "O").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "F").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "P").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "G").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "Q").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "H").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "S").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "L").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "T").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "N").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "F").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "C").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "M").Offset(1, 0).Value = newData.Worksheets("Incident List").Cells(ndRow, "E").Value
currentData.Worksheets("Incident List").Cells(cdLastRow, "M").Offset(1, 0).NumberFormat = "m/d/yyyy"
currentData.Worksheets("Incident List").Rows(cdLastRow).Offset(1, 0).Borders.LineStyle = xlContinuous
' Reset cdLastRow
currentData.Worksheets("Incident List").Activate
With ActiveSheet
cdLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
End If
Next rout
newData.Close
End Sub
您不需要兩個循環。只需要一個循環來獲取檢查的值,然後您可以使用'If Application.WorksheetFunction.CountIf(cdRangeToCheck,「Value to check」)> 0 Then' –
另一種選擇是將它們簡單地存儲在'Scripting.Dictionary' 。 – Comintern