VBA need help- Copy Rows if Cell contains - expanded

jdorfma

New Member
Joined
Jun 15, 2021
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hello all!

I am building out a workbook where every sheet is for a different stage of a software installation. I am trying to aggregate the steps that fail by copying my fail rows into a summary sheet. I finally got them to pull, but they are pulling into the new sheet on the same row # as they are located in the original sheet.

Here is what I am using now:

Option Explicit

Sub Test()

Dim Cell As Range

With Sheets(15)
' loop column H untill last cell with value (not entire column)
For Each Cell In .Range("D1:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
If Cell.Value = "Fail" Then
' Copy>>Paste in 1-line (no need to use Select)
.Rows(Cell.Row).Copy Destination:=Sheets(2).Rows(Cell.Row)
End If
Next Cell
End With

End Sub




I need=
How can I modify to:
Pull row that has cell containing "Fail"
Copy row into master starting at Row 2 and consecutively down without overwriting
Run across all sheets- they are named per step of install - do i need to rename to "sheet1, sheet2, etc"
 

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.
Welcome to the Board!

Let's start here:
Copy row into master starting at Row 2 and consecutively down without overwriting

Does the data always have an entry in column A?
If so, try changing this line:
VBA Code:
.Rows(Cell.Row).Copy Destination:=Sheets(2).Rows(Cell.Row)
to this:
VBA Code:
.Rows(Cell.Row).Copy Destination:=Sheets(2).Cells(Rows.Count,"A").End(xlUp).Offset(1,0)

This should paste it to the next available row on Sheet2.
 
Upvote 0
Welcome to the Board!

Let's start here:


Does the data always have an entry in column A?
If so, try changing this line:
VBA Code:
.Rows(Cell.Row).Copy Destination:=Sheets(2).Rows(Cell.Row)
to this:
VBA Code:
.Rows(Cell.Row).Copy Destination:=Sheets(2).Cells(Rows.Count,"A").End(xlUp).Offset(1,0)

This should paste it to the next available row on Sheet2.
A great start, thank you! I'm self teaching VBA to myself, so it's a lot of trial and error but now I have a project applying pressure.
 
Upvote 0
A great start, thank you! I'm self teaching VBA to myself, so it's a lot of trial and error but now I have a project applying pressure.
You are welcome.

So, did that fix that issue?
 
Upvote 0
Hello JDorfma,

Further to Joe's suggestion, why don't you automate the AutoFilter on each worksheet(excluding the Master sheet):-

VBA Code:
Sub Test()

Dim ws As Worksheet, sh As Worksheet
Set sh = Sheets("Master")

Application.ScreenUpdating = False
        
        'sh.UsedRange.Offset(1).Clear  'If required, this line will clear the Master sheet with each transfer of data.
        
        For Each ws In Worksheets
                If ws.Name <> "Master" Then
                        With ws.[A1].CurrentRegion
                                .AutoFilter 4, "Fail"
                                .Offset(1).EntireRow.Copy sh.Range("A" & Rows.Count).End(3)(2)
                                .AutoFilter
                        End With
                End If
        Next ws

Application.ScreenUpdating = True

End Sub

It filters on Column D in each source sheet for "Fail" (not Column H as mentioned in your code) then transfers the relevant rows of data to the Master sheet starting in ColumnA (without overwriting).

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
You are welcome.

So, did that fix that issue?

How can i get it to run across a range of worksheets(or whole workbook) now? I have about 10 I would like the macro to run across synchronously. I feel like I should know how to modify to do that but I can't get there
 
Upvote 0
How can i get it to run across a range of worksheets(or whole workbook) now? I have about 10 I would like the macro to run across synchronously. I feel like I should know how to modify to do that but I can't get there
See if vcoolio's response answers that question for you.
 
Upvote 0
See if vcoolio's response answers that question for you.
Uh, that didnt pan out for me. I really just need to expand upon yours - which is to run against all the sheets 'except' maybe the first 4
 
Upvote 0
Uh, that didnt pan out for me. I really just need to expand upon yours - which is to run against all the sheets 'except' maybe the first 4
That is not correct.
vcoolio's code will run against ALL sheets, except the one named "Master".
Just changed "Master" to the name of the sheet you do not want to run it against (the sheet where you are copying everything to).
 
Upvote 0
That is not correct.
vcoolio's code will run against ALL sheets, except the one named "Master".
Just changed "Master" to the name of the sheet you do not want to run it against (the sheet where you are copying everything to).
Right - the code is failing because of this line - but im not sure why

.AutoFilter 4, "Fail"
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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