VBA copy rows between two specific values

milito1990

New Member
Joined
Jul 5, 2016
Messages
7
Hello,
I have around 100 sheets per workbook, and i'm trying to consolidate some relevant date from each Sheet to a summary one: I was wondering if there is a way to copy just selected rows into a new sheet, one below the other.
when i try to do this individually I can manage, but what i'm trying to do is copy the first Row of each Sheet (containing the ref. name of the sheet), and then a dynamic number of rows, always delimited by the same two lines of text, but not fixed in position or length (some examples of ranges A5:H9 or A15:F23 and so on). the reference text is always in a cell in column A.
I've found this code on internet that should fit my needs, but
Code:
Public Sub getAircrew()    Dim ws As Worksheet
    Dim nRow As Long
    Dim nStart As Long, nEnd As Long
    Dim range1 As Range, range2 As Range, multipleRange As Range
    
    Application.ScreenUpdating = False
    Sheets("Master").Activate
     
    For Each ws In Worksheets
        If ws.Name <> "Master" Then
            Set range1 = ws.Range("A1:H1")
           
            For nRow = 1 To 65536
            If Range("A" & nRow).Value = "Population Values" Then
            nStart = nRow
            Exit For
            End If
            Next nRow
            
            For nRow = nStart To 65536
            [COLOR=#ff0000]If Range("A" & nRow).Value = "* Denotes the st.dev of sample" Then[/COLOR]
            nEnd = nRow
            Exit For
            End If
            Next nRow
            nEnd = nEnd - 1


            Set range2 = ws.Range("A" & nStart & ":H" & nEnd)
            
            Set multipleRange = Union(range1, range2)
            multipleRange.Copy
            
            
            ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
        End If
    Next ws

End Sub

at the highlighted line i get a error 1004 "
method range of object _global failed"
If I change the dynamic searched range with a fixed interval the code works
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this
Code:
If Range("A" & nRow).Value [COLOR="#B22222"]Like[/COLOR] "* Denotes the st.dev of sample" Then
and if that don't work then try this
Code:
If InStr(Range("A" & nRow).Value , "Denotes the st.dev of sample") > 0 Then
 
Last edited:
Upvote 0
Thanks for the reply; I tried using both codes, but I get a Run-time 1004 "Application-defined or object-definederror" each time.
 
Upvote 0
I think you need to qualify your range lookups:

Rich (BB code):
If ws.Range("A" & nRow).Value = "Population Values" Then

and

Rich (BB code):
If ws.Range("A" & nRow).Value = "* Denotes the st.dev of sample" Then

WBD
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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