使用(嵌套循环).find抓取相同的值单独的VBA EXCEL表

我有以下代码循环通过两个不同的工作表,并比较列A到列A检查相同的值是否在另一张表。 如果是的话那么这行就是绿色的。

Dim compareRange As Range Dim toCompare As Range Dim rFound As Range Dim cel As Range Set compareRange = Worksheets("sheet2").Range("A1:A" & Lastrow3) Set toCompare = Worksheets("sheet3").Range("A1:A" & Lastrow4) Set rFound = Nothing For Each cel In toCompare Set rFound = compareRange.Find(cel) If Not rFound Is Nothing Then cel.EntireRow.Interior.Color = 5296274 Set rFound = Nothing End If Next cel 

现在,我有行的单元格,如何从同一行抓取单元格,但在不同的列? 因为现在我想检查sheet2中的列L是否与sheet3中的列L匹配。 如果不是,我想要从sheet2中获取这个值,并把它放在同一列L的下一行。任何指导或帮助,将不胜感激。

这应该有助于解释如何做你以后的事情

 Private Sub compAre() Application.ScreenUpdating = False Dim sht1 As Range Dim rcell As Range Set sht1 = ThisWorkbook.Sheets("Sheet1").Range("A1:A3") For Each rcell In sht1.Cells If rcell.Value = ThisWorkbook.Sheets("Sheet2").Range("L" & rcell.Row).Value Then sht1.Rows.Interior.Color = vbBlue End If Next rcell Application.ScreenUpdating = True End Sub 

下面是一些涵盖大部分描述的代码,对匹配的单元格进行着色,并将其放入第三个工作表的L列。 那之后我还不明白剩下的问题,但是这应该给你一个好的开始。 animation开始时显示1,2,3的内容,然后在运行macros之后再次显示这些表单。

 Option Explicit Sub test() Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, num As Integer Dim r1 As Range, r2 As Range, r3 As Range, cell1 As Range, cell2 As Range Set sh1 = Worksheets("1") Set sh2 = Worksheets("2") Set sh3 = Worksheets("3") Set r1 = Range(sh1.Range("A1"), sh1.Range("A1").End(xlDown)) Set r2 = Range(sh2.Range("A1"), sh2.Range("A1").End(xlDown)) Set r3 = sh3.Range("L1") For Each cell1 In r1 For Each cell2 In r2 If cell1 = cell2 Then cell1.Interior.Color = vbGreen cell2.Interior.Color = vbGreen r3 = cell1.Value Set r3 = r3.Offset(1, 0) num = num + 1 End If Next Next MsgBox (num & " were found to match") End Sub 

在这里输入图像说明