Only the top row is being copied

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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).
 
Upvote 0
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 ?
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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"
 
Upvote 0
VBA Code:
.AutoFilter 2, "<>1000", xlAnd, "<>100"
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,974
Members
448,934
Latest member
audette89

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