VBA - Copy Filtered Range column O to another same filtered range in column M

RPA

Board Regular
Joined
May 14, 2004
Messages
55
I'm trying to write vba where column O will first filter out #N/A, and copy visible cells only (I have this code below).
Next (I can't figure out without error), I want to copy this filtered/visible range from column O to the same visible range in column M.
When I manually try: Select Range -> GoTo -> Special -> Visible Cells Only, then try to paste I get the error "action won't work on multiple selections", and I can't figure out what the code might be. Any assistance on this would be much appreciated. Thanks.

Sample Data filtered list/visible cells only. I want to paste a copy of column O into column M:
Column M Column O
Row Name Customer Name
1 Dell 02-Dell
2 Apple 03-Apple
4 Acer 04-Acer
5 Lenova 05-Lenova
6 Asus 06-Asus
12 Etc Etc


Range("O1").Select
Selection.AutoFilter Field:=15, Criteria1:="<>#N/A"
Range("O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I'm trying to write vba where column O will first filter out #N/A, and copy visible cells only (I have this code below).
Next (I can't figure out without error), I want to copy this filtered/visible range from column O to the same visible range in column M.
When I manually try: Select Range -> GoTo -> Special -> Visible Cells Only, then try to paste I get the error "action won't work on multiple selections", and I can't figure out what the code might be. Any assistance on this would be much appreciated. Thanks.

Try this:
This macro will copy-paste from a visible range to a visible range.

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] CopyVisibleToVisible1()
[I][COLOR=seagreen]'Copy paste(value):[/COLOR][/I]
[I][COLOR=seagreen]'from filtered to filtered range[/COLOR][/I]
[I][COLOR=seagreen]'from filtered to unfiltered range[/COLOR][/I]
[I][COLOR=seagreen]'from unfiltered to filtered range[/COLOR][/I]
[I][COLOR=seagreen]'Not work on hidden column[/COLOR][/I]
[I][COLOR=seagreen]'Can't copy-paste to another workbook[/COLOR][/I]

    [COLOR=Royalblue]Dim[/COLOR] rngA [COLOR=Royalblue]As[/COLOR] Range
    [COLOR=Royalblue]Dim[/COLOR] rngB [COLOR=Royalblue]As[/COLOR] Range
    [COLOR=Royalblue]Dim[/COLOR] r [COLOR=Royalblue]As[/COLOR] Range
    [COLOR=Royalblue]Dim[/COLOR] Title [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]String[/COLOR]
    [COLOR=Royalblue]Dim[/COLOR] ra [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
    [COLOR=Royalblue]Dim[/COLOR] rc [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
    
    [COLOR=Royalblue]On[/COLOR] [COLOR=Royalblue]Error[/COLOR] [COLOR=Royalblue]GoTo[/COLOR] [COLOR=Royalblue]skip[/COLOR]:
    
    Title = [COLOR=brown]"Copy Visible To Visible"[/COLOR]
    [COLOR=Royalblue]Set[/COLOR] rngA = Application.Selection
    [COLOR=Royalblue]Set[/COLOR] rngA = Application.InputBox([COLOR=brown]"Copy Range :"[/COLOR], Title, rngA.Address, [COLOR=Royalblue]Type[/COLOR]:=[COLOR=crimson]8[/COLOR])
    
    [COLOR=Royalblue]Set[/COLOR] rngB = Application.InputBox([COLOR=brown]"Paste Range (select the first cell only):"[/COLOR], Title, [COLOR=Royalblue]Type[/COLOR]:=[COLOR=crimson]8[/COLOR])
    [COLOR=Royalblue]Set[/COLOR] rngB = rngB.Cells([COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR])
    Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]

    ra = rngA.Rows.count
    rc = rngA.Columns.count
    [COLOR=Royalblue]Set[/COLOR] rngA = rngA.Cells([COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR]).Resize(ra, [COLOR=crimson]1[/COLOR])
    
    [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] r [COLOR=Royalblue]In[/COLOR] rngA.SpecialCells(xlCellTypeVisible)
      rngB.Resize([COLOR=crimson]1[/COLOR], rc).Value = r.Resize([COLOR=crimson]1[/COLOR], rc).Value
        [COLOR=Royalblue]Do[/COLOR]
          [COLOR=Royalblue]Set[/COLOR] rngB = rngB.Offset([COLOR=crimson]1[/COLOR], [COLOR=crimson]0[/COLOR])
        [COLOR=Royalblue]Loop[/COLOR] [COLOR=Royalblue]Until[/COLOR] rngB.RowHeight <> [COLOR=crimson]0[/COLOR]
    [COLOR=Royalblue]Next[/COLOR]
    
    Application.Goto rngB
    Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]
    Application.CutCopyMode = [COLOR=Royalblue]False[/COLOR]
[COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
[COLOR=Royalblue]skip[/COLOR]:
    [COLOR=Royalblue]If[/COLOR] err.Number <> [COLOR=crimson]424[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        MsgBox [COLOR=brown]"Error found: "[/COLOR] & err.Description
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
    
    Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]
    Application.CutCopyMode = [COLOR=Royalblue]False[/COLOR]


[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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