VBA Loop through Sheets

BelfastHatter

Board Regular
Joined
Nov 7, 2008
Messages
60
Hello I have a macro that should loop through the sheets until it reaches the sheet named "HO". However it still tries to run the macro on this sheet.

Please can someone let me know where I have gone wrong.

Here is my code.

Sub Nursery()


Dim ws As Worksheet


For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "HO" Then

Application.DisplayAlerts = False

ActiveSheet.Copy

Dim src As Variant
src = ActiveWorkbook.LinkSources(xlLinkTypeExcelLinks)
ActiveWorkbook.BreakLink src(1), xlLinkTypeExcelLinks


Dim path As String
Dim filename As String
path = "T:\Nursery Financial Reports" & Range("B1") & "" & Range("C1") & ""
filename = Range("B1") & " - " & Range("Q1")
ActiveWorkbook.SaveAs filename:=path & filename & ".xls", FileFormat:=xlNormal


Application.DisplayAlerts = True


ActiveWorkbook.Close


ActiveSheet.Next.Select


End If
Next ws


End Sub



Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Get rid of is line of code and please use code tags. HTH. Dave
Code:
ActiveSheet.Next.Select
ps. U should really take the alert/screen updating stuff outside of the loop. Also why are U copying the sheet in the loop?
 
Last edited:
Upvote 0
Just guessing as you haven't described what you want the code to do but what happens with...

Code:
Sub Nursery()


Dim ws As Worksheet


For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "HO" Then

Application.DisplayAlerts = False

ws.Cells.Copy

Dim src As Variant
src = ActiveWorkbook.LinkSources(xlLinkTypeExcelLinks)
ActiveWorkbook.BreakLink src(1), xlLinkTypeExcelLinks


Dim path As String
Dim filename As String
path = "T:\Nursery Financial Reports" & ws.Range("B1") & "" & ws.Range("C1") & ""
filename = ws.Range("B1") & " - " & ws.Range("Q1")
ActiveWorkbook.SaveAs filename:=path & filename & ".xls", FileFormat:=xlNormal


Application.DisplayAlerts = True


ActiveWorkbook.Close




End If
Next ws


End Sub
 
Upvote 0
Change the ws.Cells.Copy in the code I posted to ws.Copy.
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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