在下面的宏中,Sheet1中的数据将单元格“X”中的单元格与sheet2中列“A”中的数据进行比较。如果匹配,则sheet1中的整个相应行将复制到sheet3。 我需要一个和平的代码,在这个宏中只会比较前8个字符。
Sub CopyMatch()
Dim StartingScreenUpdateValue As Boolean
Dim StartingEventsValue As Boolean
Dim StartingCalculations As XlCalculation
With Application
StartingScreenUpdateValue = .ScreenUpdating
StartingEventsValue = .EnableEvents
StartingCalculations = .Calculation
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Dim varTestValues As Variant
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
Set sh3 = Sheets("Sheet3")
With sh2
varTestValues = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
End With
With sh1
.Range("X1", .Range("X" & .Rows.Count).End(xlUp)) _
.AutoFilter Field:=1, Criteria1:=Application.Transpose(varTestValues), Operator:=xlFilterValues
.Range("X2", sh1.Range("X" & .Rows.Count).End(xlUp)) _
.SpecialCells(xlCellTypeVisible).EntireRow.Copy sh3.Range("A2")
.AutoFilterMode = False
End With
With Application
.ScreenUpdating = StartingScreenUpdateValue
.EnableEvents = StartingEventsValue
.Calculation = StartingCalculations
End With
End Sub