For Next loop debugging

fklatecki

New Member
Joined
Oct 31, 2016
Messages
2
Greetings Excel VBA gurus,

I need a another set of eyes on this VBA code. The hard part to admit was that a slightly previous version was working as desired.

I am Bursting a large Sheet into individual sheets based on value in the first column. (credit to Chandoo.org for basis of this VBA code)

The code runs as planned for the first pass, but on the second run I get a debug error. Can I be directed to correct the error of my ways?



Sub breakMyList() ' This macro takes values in the range myList
' and breaks it in to multiple lists
' and saves them to separate files.

Dim cell As Range
Dim curPath As String
Dim ws As Worksheet
Dim sShtName As String

Set ws = Sheets("SupplierSales")

curPath = ActiveWorkbook.Path & ""

Application.ScreenUpdating = True
Application.DisplayAlerts = False


For Each cell In Range("Suppliers")
Sheets("SupplierSales").Select

sShtName = Sheet1.Cells(4, 2).Value


Range("myList").AdvancedFilter Action:=xlFilterCopy, _
criteriarange:=Range("Criteria"), copyToRange:=Range("Extract"), unique:=False
Range(Range("Extract"), Range("Extract").End(xlDown)).Copy


'adds worksheet
Worksheets.Add After:=Sheets(Worksheets.Count)


ActiveSheet.Name = sShtName <------------------------------debug error on second pass...That name is already taken. Try another one.

ActiveSheet.Paste

Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.CutCopyMode = False




Worksheets("SupplierSales").Activate
Range(Range("Extract"), Range("Extract").End(xlDown)).ClearContents



Next cell

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to the message board!

Your problem seems to be the line
Code:
[COLOR=#333333]sShtName = Sheet1.Cells(4, 2).Value[/COLOR]

The name is static. Replace it with a dynamic name:
Code:
sshtname = Sheet1.Cells(4, 2).Value & Sheets.Count
 
Upvote 0
I apologize for being in midst of my VBA learning curve.

The value of sShtName should be a single cell Range on Sheet(1) Label "SupplierSales", the Range name is "SheetName".

Your assistance is greatly appreciated...
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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