Paste data from select columns after applying auto filter

WildBurrow

New Member
Joined
Apr 5, 2021
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I've found code that will copy/paste select columns from a row of data that has been filtered. The code works fine if I paste to a "dummy" sheet that does not contain any merged cells.

However, my target sheet has several merged cells below the area where the pasted data is to be placed. I receive an error message that it cannot paste into merged cells (because it's looking at the entire column).

The copy range will always be limited to 12 rows and my target sheet provides 12 rows for the pasted data. Below are images of my sheets.

Source Sheet: Target Sheet:
1646076833144.png
1646076984297.png


This is the code that I have thus far:
VBA Code:
Sub CopyTabletoCOC() 'This works only if all the rows within the column are not merged
Dim ws2 As Worksheet, ws3 As Worksheet, lRow As Long

'Set Sheet reference
    Set ws2 = worksheets("Tables")
    Set ws3 = worksheets("Fake")
    
    lRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).row
    
    With ws2
    
        .range("AX1").AutoFilter Field:=1, Criteria1:="Y" 'set your filter

        'copy the visible cells in each column from row 2 and resize to the last row
        'paste to the the cell you want your copied range to start in your second worksheet
        .range("AY2").Resize(lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.range("B2") 'Collection Number
        .range("AZ2").Resize(lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.range("G2") 'Date
        .range("BA2").Resize(lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.range("J2") 'Time
        .range("BB2").Resize(lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.range("AD2") 'Latitude
        .range("BC2").Resize(lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws3.range("AH2") 'Longitude

        .range("AX1").AutoFilter 'clear the filter
     End With
End Sub

Code source from: VBA Copy and Paste Certain Columns of a sheet to specific columns on another sheet

How do I identify the target sheet range in order to avoid the merge error?

Vikki
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top