我想根據第一列值將「主」工作表與「新建」工作表進行比較。如果在「新建」工作表中可用,那麼我想比較「主」工作表的匹配行的列「E」與「新建」工作表的匹配行的列「E」。如果有任何差異,則將「主」的列值「E」替換爲「新」的列值「E」,並按顏色突出顯示整行。比較並替換excel vba宏中的列值
Sub CompareValues()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lr As Long, rng As Range, c As Range
Set sh1 = Sheets("New")
Set sh2 = Sheets("Master")
lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row 'Get last row with data in Column A on Sheet 1.
Set rng = sh2.Range("A2:A" & lr) 'Set compare range to a variable
For Each c In rng 'Make cell by cell comparison
If Application.CountIf(sh1.Range("A:A"), c.Value) <> 0 Then
If c.EntireRow.Range("E") <> sh1.Range("E", c.Value) Then
' to fill the value into another sheet simply replace from sh1 to c.Range
c.Range("E" & i).Copy (sh1.Range("E" & i))
Range(c, sh2.Cells(c.Row, Columns.Count).End(xlToLeft)).Interior.ColorIndex = 4
End If
End If
Next
End Sub
嗨marcw,對不起,對於遲到的答覆。感謝您的投入。我試過你的解決方案,如果主表第一列的數據與新表的相同行號的數據匹配,它工作正常。但我需要在新工作表中的任何位置找到它並在主工作表中進行更新。 – user1907867