loop through specific sheets and copy entire row into one master sheet if data is in column

Omer104

New Member
Joined
Oct 5, 2015
Messages
42
Office Version
  1. 365
Dear VBA wizards,

Can someone please help me – I have been going through this for a while

I have about 25 sheets in total in an excel file of which 10 sheets (one tab is called is AGV, one other tab is AGR etc) has some data and I would like to copy the entire row in each of these sheets if it has the text “new instrument” in col S, in these 10 sheets there are a lot of rows around 2000

This loop macro would collate all these rows in a sheet called “new instrument”

Can someone please help ?

Thanks a lot,
 
Hey Yongle,

Headers on Row 4
I can add a filter on Row 4 and on column S New instrument is in the dropdown list,
Data starts on Row 5
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If headers are in row 4, amend these 2 lines
Code:
.Range("A[COLOR="#FF0000"]4[/COLOR]:ZZ9999").AutoFilter Field:=19, Criteria1:="*new instrument*"
.Rows("[COLOR="#FF0000"]5[/COLOR]:9999").SpecialCells(xlCellTypeVisible).Copy Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Last edited:
Upvote 0
I assume you did not like my answer in Post 4 since you said nothing about my code.
 
Upvote 0
ah so sorry! I did respond but it didnt post!

I ran it but it keeps crashing at

.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("new instrument").Rows(Lastrowa)

Application defined or object defined error
 
Upvote 0
did you test amendment suggested in post#12 ?
 
Upvote 0
I inadvertently reverted to the previous code in post#12
Apologies for that..:oops:
Try

Code:
 .Range("A4:ZZ9999").AutoFilter Field:=19, Criteria1:="New Instrument"
 
Last edited:
Upvote 0
Here is what I am using
- the original macro amended as above (also amended the header row to be copied)

Code:
Sub NewInstrument()
    Dim Ws As Worksheet, w As Variant
                                            'Const List = "AGV,AGR,XX3,XX4,XX5,XX6,XX7,XX8,XX9,X10"
    Const List = "[COLOR=#ff0000]AGV[/COLOR]"  'for testing just one sheet
    Application.ScreenUpdating = False
    On Error Resume Next
    Set Ws = Sheets.Add(before:=Sheets(1))
    Ws.Name = "new instrument"
    For Each w In Split(List, ",")
        With Sheets(w)
            .ShowAllData
            .Range("A[COLOR=#ff0000]4[/COLOR]:ZZ9999").AutoFilter Field:=19, Criteria1:="[COLOR=#ff0000]New instrument[/COLOR]"
            .Rows("[COLOR=#ff0000]5[/COLOR]:9999").SpecialCells(xlCellTypeVisible).Copy Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Rows([COLOR=#ff0000]4[/COLOR]).Copy Ws.Range("A1")
            .ShowAllData
        End With
    Next w
End Sub

Sheet AGV

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
Some
2
random
3
text
4
H01H02H03H04H05H06H07H08H09H10H11H12H13H14H15H16H17H18H19H20
5
Data01
0​
3​
0​
3​
7​
7​
3​
8​
1​
1​
2​
5​
4​
6​
8​
5​
1​
4​
6
Data02
3​
3​
4​
7​
2​
0​
6​
5​
9​
3​
8​
2​
4​
5​
5​
4​
5​
New Instrument
6​
7
Data03
0​
2​
7​
7​
5​
5​
5​
4​
6​
4​
7​
7​
1​
5​
4​
9​
5​
New Instrument
5​
8
Data04
4​
8​
8​
4​
6​
3​
2​
9​
6​
4​
1​
7​
7​
4​
9​
5​
4​
4​
9
Data05
8​
0​
4​
0​
0​
5​
2​
1​
6​
8​
6​
9​
9​
8​
0​
4​
5​
New Instrument
1​
10
Data06
6​
0​
2​
3​
6​
8​
2​
3​
9​
3​
4​
5​
9​
8​
9​
3​
6​
New Instrument
1​
11
Data07
4​
4​
7​
7​
5​
0​
7​
0​
9​
3​
9​
5​
3​
9​
4​
3​
1​
7​
12
Data08
9​
0​
6​
3​
4​
6​
7​
8​
2​
9​
0​
1​
5​
8​
3​
4​
5​
New Instrument
1​
13
Data09
5​
5​
0​
1​
5​
9​
0​
0​
1​
4​
7​
1​
9​
0​
2​
0​
1​
5​
14
Data10
9​
4​
7​
2​
9​
1​
4​
8​
7​
4​
2​
9​
0​
9​
8​
0​
1​
9​
Sheet: AGV

Sheet "new instrument"

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
H01H02H03H04H05H06H07H08H09H10H11H12H13H14H15H16H17H18H19H20
2
Data02
3​
3​
4​
7​
2​
0​
6​
5​
9​
3​
8​
2​
4​
5​
5​
4​
5​
New Instrument
6​
3
Data03
0​
2​
7​
7​
5​
5​
5​
4​
6​
4​
7​
7​
1​
5​
4​
9​
5​
New Instrument
5​
4
Data05
8​
0​
4​
0​
0​
5​
2​
1​
6​
8​
6​
9​
9​
8​
0​
4​
5​
New Instrument
1​
5
Data06
6​
0​
2​
3​
6​
8​
2​
3​
9​
3​
4​
5​
9​
8​
9​
3​
6​
New Instrument
1​
6
Data08
9​
0​
6​
3​
4​
6​
7​
8​
2​
9​
0​
1​
5​
8​
3​
4​
5​
New Instrument
1​
Sheet: new instrument





Below is the amended macr
 
Upvote 0
Here is what I am using
- the original macro amended as above (also amended the header row to be copied)

Code:
Sub NewInstrument()
    Dim Ws As Worksheet, w As Variant
                                            'Const List = "AGV,AGR,XX3,XX4,XX5,XX6,XX7,XX8,XX9,X10"
    Const List = "[COLOR=#ff0000]AGV[/COLOR]"  'for testing just one sheet
    Application.ScreenUpdating = False
    On Error Resume Next
    Set Ws = Sheets.Add(before:=Sheets(1))
    Ws.Name = "new instrument"
    For Each w In Split(List, ",")
        With Sheets(w)
            .ShowAllData
            .Range("A[COLOR=#ff0000]4[/COLOR]:ZZ9999").AutoFilter Field:=19, Criteria1:="[COLOR=#ff0000]New instrument[/COLOR]"
            .Rows("[COLOR=#ff0000]5[/COLOR]:9999").SpecialCells(xlCellTypeVisible).Copy Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Rows([COLOR=#ff0000]4[/COLOR]).Copy Ws.Range("A1")
            .ShowAllData
        End With
    Next w
End Sub

Sheet AGV

Excel 2016 (Windows) 32 bit
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]D[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]E[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]F[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]G[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]H[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]I[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]J[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]K[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]L[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]M[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]N[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]O[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]P[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]Q[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]R[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]S[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]T[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
Some
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
random
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
text
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
H01H02H03H04H05H06H07H08H09H10H11H12H13H14H15H16H17H18H19H20
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
Data01
0​
3​
0​
3​
7​
7​
3​
8​
1​
1​
2​
5​
4​
6​
8​
5​
1​
4​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
Data02
3​
3​
4​
7​
2​
0​
6​
5​
9​
3​
8​
2​
4​
5​
5​
4​
5​
New Instrument
6​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]​
Data03
0​
2​
7​
7​
5​
5​
5​
4​
6​
4​
7​
7​
1​
5​
4​
9​
5​
New Instrument
5​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]8[/COLOR]​
Data04
4​
8​
8​
4​
6​
3​
2​
9​
6​
4​
1​
7​
7​
4​
9​
5​
4​
4​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]9[/COLOR]​
Data05
8​
0​
4​
0​
0​
5​
2​
1​
6​
8​
6​
9​
9​
8​
0​
4​
5​
New Instrument
1​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]10[/COLOR]​
Data06
6​
0​
2​
3​
6​
8​
2​
3​
9​
3​
4​
5​
9​
8​
9​
3​
6​
New Instrument
1​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]11[/COLOR]​
Data07
4​
4​
7​
7​
5​
0​
7​
0​
9​
3​
9​
5​
3​
9​
4​
3​
1​
7​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]12[/COLOR]​
Data08
9​
0​
6​
3​
4​
6​
7​
8​
2​
9​
0​
1​
5​
8​
3​
4​
5​
New Instrument
1​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]13[/COLOR]​
Data09
5​
5​
0​
1​
5​
9​
0​
0​
1​
4​
7​
1​
9​
0​
2​
0​
1​
5​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]14[/COLOR]​
Data10
9​
4​
7​
2​
9​
1​
4​
8​
7​
4​
2​
9​
0​
9​
8​
0​
1​
9​

<tbody>
</tbody>
Sheet: AGV

<tbody>
</tbody>

Sheet "new instrument"

Excel 2016 (Windows) 32 bit
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]D[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]E[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]F[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]G[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]H[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]I[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]J[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]K[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]L[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]M[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]N[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]O[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]P[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]Q[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]R[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]S[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]T[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
H01H02H03H04H05H06H07H08H09H10H11H12H13H14H15H16H17H18H19H20
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
Data02
3​
3​
4​
7​
2​
0​
6​
5​
9​
3​
8​
2​
4​
5​
5​
4​
5​
New Instrument
6​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
Data03
0​
2​
7​
7​
5​
5​
5​
4​
6​
4​
7​
7​
1​
5​
4​
9​
5​
New Instrument
5​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
Data05
8​
0​
4​
0​
0​
5​
2​
1​
6​
8​
6​
9​
9​
8​
0​
4​
5​
New Instrument
1​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
Data06
6​
0​
2​
3​
6​
8​
2​
3​
9​
3​
4​
5​
9​
8​
9​
3​
6​
New Instrument
1​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
Data08
9​
0​
6​
3​
4​
6​
7​
8​
2​
9​
0​
1​
5​
8​
3​
4​
5​
New Instrument
1​

<tbody>
</tbody>
Sheet: new instrument

<tbody>
</tbody>





Below is the amended macr

Thank you so much it works!!
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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