Only the top row is being copied

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
I have here my code that does not run properly because when I press F5 it only copies the data from the top row, but when I do the step into (F8) it works fine. Is there something I should modify on the code below?


VBA Code:
Sub CopyData()
'
'
' Keyboard Shortcut: Ctrl+Shift+Q
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
   
    Sheets(1).Activate
    ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=1, Criteria1:="High"
    ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=2, Criteria1:="1000"
    ActiveSheet.Range("A1:D" & LR).AutoFilter Field:=3, Criteria1:="10"
    Range("A1:D" & LR).Select
    Selection.Copy
    Sheets("Class1").Select
    ActiveSheet.Paste
    Sheets(1).Select
    ActiveSheet.Range("$A$1:D" & LR).AutoFilter Field:=3, Criteria1:="11"
    Range("A1:D" & LR).Select
    Selection.Copy
    Sheets("Class2").Select
    ActiveSheet.Paste

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try putting the LR = line below the Sheets(1).Activate line
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
You're welcome. When I get back in I'll post some code to do away with those Selects and Activates to make the code a bit more efficient (a bit of a pain to code on a phone).
 

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

You're welcome. When I get back in I'll post some code to do away with those Selects and Activates to make the code a bit more efficient (a bit of a pain to code on a phone).
That's nice, thanks. Btw, Im learning to code thru macro recording (just a newbie) and I'm just experimenting on how to prepare my reports easily 😅
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try the code below, please note that you might have to reset the keyboard shortcut.
Btw, using the recorder isn't the worst way of learning basic VBA syntax ;)

VBA Code:
Sub CopyData2()
    '
    '
    ' Keyboard Shortcut: Ctrl+Shift+Q
    Dim LR As Long
    Application.ScreenUpdating = False
 
    With Sheets(1)
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        With .Range("A1:D" & LR)
            .AutoFilter 1, "High"
            .AutoFilter 2, "1000"
            .AutoFilter 3, "10"
            .Copy Sheets("Class1").Cells(1, "A")
            .AutoFilter 3, "11"
            .Copy Sheets("Class2").Cells(1, "A")
        End With
    End With
   
    Application.ScreenUpdating = True
End Sub
 

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Try the code below, please note that you might have to reset the keyboard shortcut.
Btw, using the recorder isn't the worst way of learning basic VBA syntax ;)

VBA Code:
Sub CopyData2()
    '
    '
    ' Keyboard Shortcut: Ctrl+Shift+Q
    Dim LR As Long
    Application.ScreenUpdating = False

    With Sheets(1)
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        With .Range("A1:D" & LR)
            .AutoFilter 1, "High"
            .AutoFilter 2, "1000"
            .AutoFilter 3, "10"
            .Copy Sheets("Class1").Cells(1, "A")
            .AutoFilter 3, "11"
            .Copy Sheets("Class2").Cells(1, "A")
        End With
    End With
  
    Application.ScreenUpdating = True
End Sub
Thanks for this. However I have a follow up question, on this line
VBA Code:
 .AutoFilter 2, "1000"
I tried to add another criteria which looks like this
VBA Code:
 .AutoFilter 2, "1000", "1500"
and it results to run time error. Is there a proper way to add criteria and is it only limited up to 2 only?
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try

VBA Code:
.AutoFilter 2, "1000", xlOr, "100"

and yes you are restricted to 2 criteria when using a regular autofilter. You would either use an Array or Advanced filter if you needed more criteria.
 

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
102
Office Version
  1. 2019
Platform
  1. Windows
Try

VBA Code:
.AutoFilter 2, "1000", xlOr, "100"

and yes you are restricted to 2 criteria when using a regular autofilter. You would either use an Array or Advanced filter if you needed more criteria.
What if I use the "does not contain criteria"?
This is what I tried and it gives error
VBA Code:
.AutoFilter 2, "<>1000", xlOr, "<>1500"
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
VBA Code:
.AutoFilter 2, "<>1000", xlAnd, "<>100"
 

Watch MrExcel Video

Forum statistics

Threads
1,129,589
Messages
5,637,278
Members
416,963
Latest member
samfuge

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
Top