limit the number of rows copied over

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, I am using the following code to sort data in a WS and copy it over to another WS. It all works just fine but I was thinking that I would like to limit it the first "X" numbers of rows so that my second WS doesn't get too big - lets say the first 5 entries. So I am thinking that I want to put a counter on each time it copies, and limit that number in End If statement. Is that the way to do it? I tried a couple of things but I can't tell if I am using the wrong syntax or if I am just wrong in the first place - I always appreciate your input.

I really try to do this on my own before I bug you all with it :).

VBA Code:
Sub CopyEpipro()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    Dim setpoint As Integer
    
    Set Source = ActiveWorkbook.Worksheets("EPIPRO")
                     
    Set Target = ActiveWorkbook.Worksheets("Export")
                   
    setpoint = ActiveWorkbook.Worksheets("Coding").Range("C2")
    
    j = 45     ' Start copying to row # in target sheet
    For Each c In Source.Range("H5:H120")   ' The column for the micron reading
        If c >= setpoint Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
    Next c
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Here is one way

Rich (BB code):
Sub CopyEpipro()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    Dim setpoint As Integer
    Dim StopAt As Long
    
    Const NumToCopy As Long = 5
   
    Set Source = ActiveWorkbook.Worksheets("EPIPRO")
                    
    Set Target = ActiveWorkbook.Worksheets("Export")
                  
    setpoint = ActiveWorkbook.Worksheets("Coding").Range("C2")
   
    j = 45     ' Start copying to row # in target sheet
    StopAt = j + NumToCopy
    For Each c In Source.Range("H5:H120")   ' The column for the micron reading
        If c >= setpoint Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
        If j = StopAt Then Exit For
    Next c
End Sub
 
Upvote 0
Solution
Here is one way

Rich (BB code):
Sub CopyEpipro()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    Dim setpoint As Integer
    Dim StopAt As Long
    
    Const NumToCopy As Long = 5
  
    Set Source = ActiveWorkbook.Worksheets("EPIPRO")
                   
    Set Target = ActiveWorkbook.Worksheets("Export")
                 
    setpoint = ActiveWorkbook.Worksheets("Coding").Range("C2")
  
    j = 45     ' Start copying to row # in target sheet
    StopAt = j + NumToCopy
    For Each c In Source.Range("H5:H120")   ' The column for the micron reading
        If c >= setpoint Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
        If j = StopAt Then Exit For
    Next c
End Sub
Thanks Peter - this worked perfectly. So you declared a constant and assigned it the limit, and then created a "counter" by making the variable "StopAt" do the counting; then count up the "j's" as it were. I think I get it (sort of, lol). Thank you for another training session.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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