我在使用VBA时遇到问题,因为我试图创建一个代码,该代码将首先基于另一个选项卡上的数据进行过滤,然后在此过滤范围内,它将在一个列上查看所有出现两次的所有值(我需要所有这些金额都用颜色标记,而不仅仅是重复的颜色。
因此,基本上可以说,在Sheet2中,我在A列代码列表中需要在Sheet1-A列中一一过滤。 然后,基于此过滤,我需要在B列中检查出现两次的值,并用绿色(仅在可见的单元格上)为这些值上色。
Sub filter()
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim xrow As Long, firstrow As Long, lastrow As Long
Dim SearchValue As Variant
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
xrow = 2
Do
SearchValue = ws2.Cells(xrow, 1)
ws.Range("$A$1:$B$14").AutoFilter Field:=1, Criteria1:=SearchValue
With ws.AutoFilter.Range
firstrow = ws.Range("A" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Row
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
'This is where the problem is
'how to match and make a color for each amount that appears twice in column B in this filtered range
End With
xrow = xrow + 1
Loop Until ws2.Cells(xrow, 1) = ""
End Sub
编辑:我最终没有做任何过滤,这对我有用,以防万一有人怀疑:
xrow = 2
Do
SearchValue = ws2.Cells(xrow, 1)
lastrow = ws.UsedRange.Rows.Count
For x = 2 To lastrow
For y = x + 1 To lastrow
If ws.Cells(x, 1) = SearchValue And ws.Cells(y, 1) = SearchValue And ws.Cells(x, 2) = ws.Cells(y, 2) Then
ws.Cells(x, 2).Interior.Color = vbGreen
ws.Cells(y, 2).Interior.Color = vbGreen
Else
End If
Next y
Next x
xrow = xrow + 1
Loop Until ws2.Cells(xrow, 1) = ""
结果是宏将检查所有代码是否具有相同的值,而我会将那些代码标记为绿色。 因此,如果在A列的Sheet1中,我有2个完全相同的代码(例如1111),并且它们具有相同的值-30,那么这30行将在两行中都用绿色标记,但是如果代码1111也具有25,则不会用颜色标记。