Renaming Worksheets

jarvisaurus

Board Regular
Joined
Nov 26, 2010
Messages
52
I have a code to rename worksheets based on a cell value but I would like to rename subsequent sheets the next unique value.

First sheet equals APPLES, then BANANAS...so forth


APPLES
APPLES
APPLES
BANANA
BANANA
BANANA

PHP:
Sub RWS()
 
 
Application.ScreenUpdating = False
For i = 1 To Sheets.Count
 
If Worksheets(i).Range("A2").Value <> "" Then
Sheets(i).Name = Worksheets(i).Range("A2").Value
 
End If
Next
End Sub

I tried Offset to no avail...the current code goes on error renaming the subsequent worksheets "APPLES"

Thanks in advance.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
/PHP]I tried Offset to no avail...the current code goes on error renaming the subsequent worksheets "APPLES"/QUOTE]

No it cannot possibly do that - it would error. Try adding

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
then test for the existence of a name

Code:
if not worksheetexists(name) then activesheet.name=nane
 
Upvote 0
I received a compile error:

"ByRef argument type mismatch" on

PHP:
If Not WorksheetExists(Name) Then ActiveSheet.Name = Name

I'm not too vba savvy...I added the function before the subroutine. Then added the above to the subroutine. Is that correct?
 
Upvote 0
Try this

Code:
Sub RWS()
Dim i As Long, j As Long, LR As Long
LR = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To Sheets.Count
    j = j + 1
    Do While WorksheetExists(Sheets(1).Range("A" & j).Value)
        j = j + 1
        If j > LR Then Exit Sub
    Loop
    Sheets(i).Name = Sheets(1).Range("A" & j).Value
Next i
End Sub

Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

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