Я попытался написать код ниже, чтобы сравнить столбец A и столбец B на 2 листе, но мой код не работает.
При сравнении столбца A я могу получить 3 возможных результата:
- Элемент найден в "Обновлении", но не в "Исходном"
- Элемент найден в "Исходном"но не в" Обновить "
- блан, когда есть совпадение
Когда сравнивается B, я могу получить 2 возможных результата:
- Элемент измененв обновлении
- В противном случае он пуст
Не могли бы вы помочь мне исправить код ниже.
Public Sub matchRow()
Dim dumpSheet, icdSheet, outputSheet As Worksheet
Dim startRow, outputRow, tempDumpRow, tempICDRow, icdRowCount, finishedICDIndex As Integer
Dim finishedICD() As String
Dim isExist As Boolean
'Set sheets
Set dumpSheet = Sheets("Update")
Set icdSheet = Sheets("Original")
Set outputSheet = Sheets("Output")
'Set start row of each sheet for data
startRow = 1
outputRow = 1
'Get row count from ICD sheet
icdRowCount = icdSheet.Range("A:B").End(xlDown).Row
'Set index
finishedICDIndex = 0
'Re-define array
ReDim finishedICD(0 To icdRowCount - 1)
'Set the start row
tempDumpRow = startRow
'Here I looped with OR state, you can modify it to AND start if you want
Do While dumpSheet.Range("A" & tempDumpRow) <> "" Or dumpSheet.Range("B" & tempDumpRow) <> "" Or dumpSheet.Range("C" & tempDumpRow) <> ""
'Reset exist flag
isExist = False
'loop all row in ICD sheet
For tempICDRow = 1 To icdRowCount Step 1
'If row is not finished for checking.
If UBound(Filter(finishedICD, tempICDRow)) < 0 Then
'If all cells of column A are equal
If dumpSheet.Range("A" & tempDumpRow) = icdSheet.Range("A" & tempICDRow) Then
'Set true to exist flag
isExist = True
'Store finished row
finishedICD(finishedICDIndex) = tempICDRow
finishedICDIndex = finishedICDIndex + 1
'If all cells of column B are equal
If dumpSheet.Range("B" & tempDumpRow) = icdSheet.Range("B" & tempICDRow) Then
'Set true to exist flag
isExist = True
'Store finished row
finishedICD(finishedICDIndex) = tempICDRow
finishedICDIndex = finishedICDIndex + 1
'exit looping
Exit For
End If
End If
End If
Next tempICDRow
'Show result column A
outputSheet.Range("A" & outputRow) = dumpSheet.Range("A" & tempDumpRow)
If isExist Then
outputSheet.Range("D" & outputRow) = ""
Else
outputSheet.Range("D" & outputRow) = "Item found in ""Update"" but not in ""Original"""
End If
'Show result column B
outputSheet.Range("B" & outputRow) = dumpSheet.Range("B" & tempDumpRow)
If isExist Then
outputSheet.Range("D" & outputRow) = ""
Else
outputSheet.Range("D" & outputRow) = "Country not the same in ""Update"" than in ""Original"""
End If
'increase output row
outputRow = outputRow + 1
'go next row
tempDumpRow = tempDumpRow + 1
Loop
'loop all row in ICD sheet
For tempICDRow = 1 To icdRowCount Step 1
'If row is not finished for checking.
If UBound(Filter(finishedICD, tempICDRow)) < 0 Then
'Show result
outputSheet.Range("A" & outputRow) = icdSheet.Range("A" & tempICDRow)
outputSheet.Range("D" & outputRow) = "Item found in ""Original"" but not in ""Update"""
'increase output row
outputRow = outputRow + 1
End If
Next tempICDRow
End Sub