-2

I'm new to Visual Basic and I want help to try something.

I want to compare all A columns from Sheet1 to A columns from Sheet2 and if exact match is found than copy the entire matching rows from Sheet2 to Sheet3.

Thanks in advance.

redbadfox
  • 3
  • 2
  • 2
    Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. http://stackoverflow.com/help/on-topic – Doug Glancy Oct 11 '13 at 14:04
  • You can start [Here](http://stackoverflow.com/questions/19227321/remove-entire-row-based-on-match/19228235#19228235) And [Here](http://stackoverflow.com/questions/19234328/if-single-cell-value-is-found-in-a-range-then-delete-entire-row/19234596#19234596) Simply change it from deleting the rows, to copy then to your destination. – user2140261 Oct 11 '13 at 14:17

1 Answers1

1

Modified version of my answer from This Question

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("A1", .Range("A" & .Rows.Count).End(xlUp)) _
    .AutoFilter Field:=1, Criteria1:=Application.Transpose(varTestValues), Operator:=xlFilterValues

    .Range("A2", sh1.Range("A" & .Rows.Count).End(xlUp)) _
    .SpecialCells(xlCellTypeVisible).EntireRow.Copy sh3.Range("A1")

    .AutoFilterMode = False
End With

With Application
    .ScreenUpdating = StartingScreenUpdateValue
    .EnableEvents = StartingEventsValue
    .Calculation = StartingCalculations
End With

End Sub

NOTE: This code runs assuming your data has headers if it does not please advise.

REMEMBER Always run any code on a copy of your data and not your actual data until you are confident that it is working 100%.

Community
  • 1
  • 1
user2140261
  • 7,855
  • 7
  • 32
  • 45
  • So, if I want to delete all the matching rows copied from Sheet1 using Autofilter? I'm trying with .EntireRow.Delete but id doesnt deletes all the rows. – redbadfox Oct 14 '13 at 17:22