Run Macro on all sheets but one

CPAgirl

New Member
Joined
Jan 17, 2022
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
I need help getting a macro to work across all of my worksheets except one. Currently the macro checks the age of consumer files on sheet 10 (not sure why the number of the sheet is 10) to see if they are over 3 years old. It then copies those files over 3 years old onto Sheet 1, which is titled "Over 3 Years". However, sometimes I have more than one sheet of data....each sheet relating to a different bank account. How do I get the macro to run through all sheets of data except for Sheet 1? Here is my code:

Sub Over3Years()

Dim rng As Range
Dim cell As Range
Dim lr As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = Sheet10
Set ws2 = Sheet1

lr = ws1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = ws1.Range("F16:F" & lr)

For Each cell In rng
If cell.Value >= 3 Then
cell.Offset(0, -5).Resize(1, 4).Copy
If ws2.Range("A11").Value = "" Then
ws2.Range("A11").PasteSpecial xlPasteValues
Else
Cells((Cells(Rows.Count, 2).End(xlUp).Row) + 1, "A").PasteSpecial xlPasteValues
End If
End If
Next cell

End Sub

I tried adding this code:

dim ws as worksheet
for each ws in thisworkbook.worksheets
if ws.name <> "sheet name" then
'your code
end if
next ws
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I need help getting a macro to work across all of my worksheets except one. Currently the macro checks the age of consumer files on sheet 10 (not sure why the number of the sheet is 10) to see if they are over 3 years old. It then copies those files over 3 years old onto Sheet 1, which is titled "Over 3 Years". However, sometimes I have more than one sheet of data....each sheet relating to a different bank account. How do I get the macro to run through all sheets of data except for Sheet 1? Here is my code:

Sub Over3Years()

Dim rng As Range
Dim cell As Range
Dim lr As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = Sheet10
Set ws2 = Sheet1

lr = ws1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = ws1.Range("F16:F" & lr)

For Each cell In rng
If cell.Value >= 3 Then
cell.Offset(0, -5).Resize(1, 4).Copy
If ws2.Range("A11").Value = "" Then
ws2.Range("A11").PasteSpecial xlPasteValues
Else
Cells((Cells(Rows.Count, 2).End(xlUp).Row) + 1, "A").PasteSpecial xlPasteValues
End If
End If
Next cell

End Sub

I tried adding this code:

dim ws as worksheet
for each ws in thisworkbook.worksheets
if ws.name <> "sheet name" then
'your code
end if
next ws
Sorry this posted before I was done. Anyway, when I added that code it gives me an error message about the Next WS statement. I am sure that I have inserted it incorrectly.

Also, this code runs very slow. Is there anything in the code that is slowing it down? And it continues to run after the data has been retrieved. Any suggestions?



Thank in advance!!
 
Upvote 0
Try the modified code below.

VBA Code:
Sub Over3Years()

Dim rng As Range
Dim cell As Range
Dim lr As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rOut As Range

'Set ws1 = Sheet10
Set ws2 = ThisWorkbook.Worksheets("Over 3 Years")
lr = ws2.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
Set rOut = ws2.Cells(lr, 1)

For Each ws1 In ThisWorkbook.Worksheets
    If ws.Name <> ws2.Name Then
        lr = ws1.Cells(Rows.Count, 1).End(xlUp).Row
        Set rng = ws1.Range("F16:F" & lr)
        
        For Each cell In rng
            If cell.Value >= 3 Then
                cell.Offset(0, -5).Resize(1, 4).Copy
                rOut.PasteSpecial xlPasteValues
                Set rOut = rOut.Cells(2, 1)
'                If ws2.Range("A11").Value = "" Then
'                    ws2.Range("A11").PasteSpecial xlPasteValues
'                Else
'                    Cells((Cells(Rows.Count, 2).End(xlUp).Row) + 1, "A").PasteSpecial xlPasteValues
'                End If
            End If
        Next cell
    End If
Next ws1

End Sub

Hope that helps!

Regards,
Ken
 
Upvote 0
It appears that I had a mistake in the code above a "ws" that should be "ws1" as in the updated code below.

VBA Code:
Sub Over3Years()

Dim rng As Range
Dim cell As Range
Dim lr As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rOut As Range

'Set ws1 = Sheet10
Set ws2 = ThisWorkbook.Worksheets("Over 3 Years")
lr = ws2.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
Set rOut = ws2.Cells(lr, 1)

For Each ws1 In ThisWorkbook.Worksheets
    If ws1.Name <> ws2.Name Then
        lr = ws1.Cells(Rows.Count, 1).End(xlUp).Row
        Set rng = ws1.Range("F16:F" & lr)
        
        For Each cell In rng
            If cell.Value >= 3 Then
                cell.Offset(0, -5).Resize(1, 4).Copy
                rOut.PasteSpecial xlPasteValues
                Set rOut = rOut.Cells(2, 1)
'                If ws2.Range("A11").Value = "" Then
'                    ws2.Range("A11").PasteSpecial xlPasteValues
'                Else
'                    Cells((Cells(Rows.Count, 2).End(xlUp).Row) + 1, "A").PasteSpecial xlPasteValues
'                End If
            End If
        Next cell
    End If
Next ws1

End Sub

Sorry for any confusion.

Regards,
Ken
 
Upvote 0
Solution
Yes, I was able to figure that out. Thank you so much for your help. I tweaked it a little bit and it works perfectly.

Thanks again!!!
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,528
Members
449,316
Latest member
sravya

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