Loop will not work

Defclone

New Member
Joined
Oct 29, 2010
Messages
3
Guys

Im new to VBA but Im currently working on a model that generates some statements for customers based on revenue and market share etc. The name of the customers change from month to month, I have a sheet where I dump the data and the model pulls all the data from this sheet. Since there are some 40 or so sheets I wanted dynamic tab names, I pieced together some code but it seems that the Next loop will not work. I can get the macro to work for the active sheet but not for the whole workbook. It can be the code or maybe I messed something up when I assigned a control-button. I would like the tab name to be "I1" since this cell corresponds to a name in a list in the input data-sheet
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Sub FANE_NAVNEIII()<o:p></o:p>
Dim ws As Worksheet<o:p></o:p>
<o:p></o:p>
For Each ws In ActiveWorkbook.Sheets<o:p></o:p>
Set Target = Range("I1")<o:p></o:p>
If Target = "" Then Exit Sub<o:p></o:p>
On Error GoTo Badname<o:p></o:p>
ActiveSheet.Name = Left(Target, 31)<o:p></o:p>
Exit Sub<o:p></o:p>
Badname:<o:p></o:p>
MsgBox "Please revise the entry in A1." & Chr(13) _<o:p></o:p>
& "It appears to contain one or more " & Chr(13) _<o:p></o:p>
& "illegal characters." & Chr(13)<o:p></o:p>
Range("I1").Activate<o:p></o:p>
<o:p></o:p>
Next ws<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
I also need a macro that saves each sheet as a separate workbook with values only using the tab name for file name.<o:p></o:p>
<o:p> </o:p>
Home some one can help as my deadline is closing in J
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try like this

Code:
Sub FANE_NAVNEIII()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
    With ws
        .Name = Left(.Range("I1").Value, 31)
    End With
Next ws
End Sub
 
Upvote 0
Hey VoG

Thanks a bunch drinks are on me :) Just trying to understand what I did wrong, guess I did not reference the ws right?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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