Dear All,
I am a beginner in VBA and I would need some help.
I am trying to write a code to copy all rows for which the cell in column A is in yellow, do this in all worksheets and paste these rows one after the other in a sheet called "PastedRows".
This sheet "PastedRows" is deleted at the beginning of the code and re-created every time the code is run.
I have found some code and tried to adapt it. The issue I am facing is with the loop. Everytime the loop is changing sheet, the variable lNewRow is reset, which is not what I want.
In other words, I would like to have the pasted rows of each sheet being pasted below the last rows already pasted in "PastedRows". Currently the code is overwriting the first rows every time the code changed worksheet.
Here is the code:
Option Explicit
Sub loop_through_all_worksheets()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Dim wks As Worksheet
Dim wNew As Worksheet
Dim lRow As Long
Dim lNewRow As Long
Dim x As Long
'Delete sheet PastedRows
Sheets("PastedRows").Delete
'Create sheet Pasted Rows
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "PastedRows"
'Set active sheet
Set starting_ws = ActiveSheet
'Loop to copy paste all rows in yellow and paste them in sheet "PastedRows"
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
Set wNew = Worksheets("PastedRows")
lNewRow = 1
For x = 1 To lRow
If wks.Cells(x, 1).Interior.Color = vbYellow Then
wks.Cells(x, 1).EntireRow.Copy wNew.Cells(lNewRow, 1)
lNewRow = lNewRow + 1
End If
Next
Next
'Activate the worksheet that was originally active
starting_ws.Activate
End Sub
I am a beginner in VBA and I would need some help.
I am trying to write a code to copy all rows for which the cell in column A is in yellow, do this in all worksheets and paste these rows one after the other in a sheet called "PastedRows".
This sheet "PastedRows" is deleted at the beginning of the code and re-created every time the code is run.
I have found some code and tried to adapt it. The issue I am facing is with the loop. Everytime the loop is changing sheet, the variable lNewRow is reset, which is not what I want.
In other words, I would like to have the pasted rows of each sheet being pasted below the last rows already pasted in "PastedRows". Currently the code is overwriting the first rows every time the code changed worksheet.
Here is the code:
Option Explicit
Sub loop_through_all_worksheets()
Dim ws As Worksheet
Dim starting_ws As Worksheet
Dim wks As Worksheet
Dim wNew As Worksheet
Dim lRow As Long
Dim lNewRow As Long
Dim x As Long
'Delete sheet PastedRows
Sheets("PastedRows").Delete
'Create sheet Pasted Rows
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "PastedRows"
'Set active sheet
Set starting_ws = ActiveSheet
'Loop to copy paste all rows in yellow and paste them in sheet "PastedRows"
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
Set wNew = Worksheets("PastedRows")
lNewRow = 1
For x = 1 To lRow
If wks.Cells(x, 1).Interior.Color = vbYellow Then
wks.Cells(x, 1).EntireRow.Copy wNew.Cells(lNewRow, 1)
lNewRow = lNewRow + 1
End If
Next
Next
'Activate the worksheet that was originally active
starting_ws.Activate
End Sub