Status
Not open for further replies.

Yup

New Member
Joined
Sep 6, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi, I have to copy rows from one sheet to other in a same excel file. It's a repetitive task. Consider following example.

NameDevicescode
ADesktop234
BLaptop2345
CLaptop3456
DSmartphone7654
EPrinter3456
FDesktop098
GScanner123
HLaptop65
ISmartphone876
JLaptop345

I want to filter "Devices". Unselect "Select All", Select "Printer" & "Scanner". copy the rows. paste it in another sheet.
But sometimes "Scanner" may not even be present in "Devices" column. In that scenario, I want to copy rows containing "Printer" & paste.
I need a same code that will work in both scenarios.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The following assumes your first row is the header row and that your actual data starts in cell A2. Change the sheet names & destination to suit.

VBA Code:
Option Explicit
Sub Copy_Array()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ change to suit
    Set ws2 = Worksheets("Sheet2")  '<~~ change to suit
    
    With ws1.Range("A1").CurrentRegion
        .AutoFilter 2, Array("Printer", "Scanner"), 7
        If ws1.Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A2") '<~~ change destination to suit
        End If
        .AutoFilter
    End With

End Sub
 
Upvote 1
Solution
Status
Not open for further replies.

Forum statistics

Threads
1,214,659
Messages
6,120,781
Members
448,992
Latest member
prabhuk279

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