Does not Loop like it is supposed to

KAOS137

New Member
Joined
May 14, 2017
Messages
8
I have the following code and I want it to loop through all sheets in the workbook except a sheet called "Summary".

Here is the code:
Sub DragSort()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

lastrow = Range("A3").End(xlDown).Row
Range("H3:I3").AutoFill Destination:=Range(Range("H3"), Range("I" & lastrow))


ActiveSheet.Name = ActiveSheet.Range("I3") & "." & Range("H3")


Next ws



End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I have the following code and I want it to loop through all sheets in the workbook except a sheet called "Summary".

Here is the code:
Sub DragSort()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

lastrow = Range("A3").End(xlDown).Row
Range("H3:I3").AutoFill Destination:=Range(Range("H3"), Range("I" & lastrow))

ActiveSheet.Name = ActiveSheet.Range("I3") & "." & Range("H3")

Next ws
End Sub
As you loop through the worksheets, they are not activated, so you need to have each range call reference the ws variable that is iterating the worksheets. So, the first line in the For..Next loop should look like this...

lastrow = ws.Range("A3").End(xlDown).Row

You need to do this same thing to each range call within the For..Next loop that is meant to apply to the currently iterated worksheet (if the ws. reference is omitted, the range call is to the active worksheet).
 
Last edited:
Upvote 0
So I made some changes but I don't think it helped. I get an error code.


-- removed inline image ---


Sub DragSort()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

lastrow = ws.Range("A3").End(xlDown).Row
ws.Range("H3:I3").AutoFill Destination:=ws.Range(Range("H3"), Range("I" & lastrow))


ActiveSheet.Name = ActiveSheet.Range("I3") & "." & Range("H3")


Next ws



End Sub
 
Upvote 0

Forum statistics

Threads
1,215,139
Messages
6,123,259
Members
449,093
Latest member
Vincent Khandagale

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