For each loop through multiple worksheets with specific exclusions

Robert E Lee

Active Member
Joined
Aug 10, 2005
Messages
266
I have the following code which works perfectly. What I now wish to do is loop through all worksheet sin Wb except sheet1. I have tried to use a For Each Loop and an If statement trying to exclude Sheet1 but it wil not compile with the message that I have a next without a for. I have tried many permutations without success.Aany help appreciated


Code:
Wb.Sheets("Sheet2").Select
                          
R = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
BudgetCode = Wb.Sheets("Sheet2").Range("A2")

Wb2.Activate
Range("D2").Select

R1 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
Set TargetRange = Range(ActiveCell, ActiveCell.Offset(R, 0))
For Each c In TargetRange
                            If c = BudgetCode Then
                            
                            Set SelectRange = Range(c.Offset(0, -2), (c.Offset(0, 3)))
                            
                            SelectRange.Copy Destination:=Wb.Sheets("Sheet2").Range("A" & R)
                            R = R + 1
                            End If
                
Next c

End Sub

Many thanks with apologies for forgetting the code tags

Rob
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Can you show us what you tried when you got the error?
 
Upvote 0
Hi Fluff

Many thanks for your response.

I think I have solved the problem. My mistake was in putting too many Nexts and Ifs making the whole thing more complex than it needed to be

This was my solution
Code:
For Each rs In Wb.Sheets

              If rs.Name <> "Sheet1" Then
              
    R = rs.Range("A1").SpecialCells(xlCellTypeLastCell).Row
 BudgetCode = rs.Range("A2")

Wb2.Activate
Range("D2").Select

R1 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
Set TargetRange = Range(ActiveCell, ActiveCell.Offset(R, 0))
For Each c In TargetRange
                            If c = BudgetCode Then
                            
                            Set SelectRange = Range(c.Offset(0, -2), (c.Offset(0, 3)))
                            
                            SelectRange.Copy Destination:=rs.Range("A" & R)
                            R = R + 1
                            End If
 
Next c
End If
Next rs

Thanks again

Rob
 
Last edited by a moderator:
Upvote 0
Glad you figured it out & thanks for the feedback.

PS
with code tags the 2nd tag should be [/code] rather than
Code:
 
Upvote 0

Forum statistics

Threads
1,215,193
Messages
6,123,560
Members
449,108
Latest member
rache47

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