RENAME SHEET WITH CELL VALUE UNTIL BRACKETS

xenios

Board Regular
Joined
Sep 4, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hi, hope somebody can help me.
Need to rename workbook sheets with cell value using macros, as it's about 300 sheets.
The problem is I have for example the name TEST1 (125) MY NAME, and need the sheet to be renamed only withe text before the 1st bracket.
Is there a way to do it with macros?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
VBA Code:
Sub xenios()
Dim wsCount As Integer, I As Long
wsCount = ActiveWorkbook.Worksheets.Count
For I = 1 To wsCount
    Worksheets(I).Name = ExtractNthWord(Worksheets(I).Name, 1)
Next I
End Sub

Function ExtractNthWord(x As String, y As Integer) 'https://www.exceltip.com/tips/how-to-extract-nth-word-from-text-string-using-vba-in-microsoft-excel-2010.html
Dim word() As String, wordCount As Long
word = VBA.Split(x, " ")
wordCount = UBound(word)
If wordCount < 1 Or (y - 1) > wordCount Or y < 0 Then
    ExtractNthWord = ""
Else
    ExtractNthWord = word(y - 1)
End If
End Function
 
Upvote 0
Hi & welcome to MrExcel.
Which cells are the new names in & which cells are the current names in?
 
Upvote 0
The name in the cell TEST1 (125) MY NAME
The name tha should have the sheet TEST1

Thank you
 
Upvote 0
VBA Code:
Sub xenios()
Dim wsCount As Integer, I As Long
wsCount = ActiveWorkbook.Worksheets.Count
For I = 1 To wsCount
    Worksheets(I).Name = ExtractNthWord(Worksheets(I).Name, 1)
Next I
End Sub

Function ExtractNthWord(x As String, y As Integer) 'https://www.exceltip.com/tips/how-to-extract-nth-word-from-text-string-using-vba-in-microsoft-excel-2010.html
Dim word() As String, wordCount As Long
word = VBA.Split(x, " ")
wordCount = UBound(word)
If wordCount < 1 Or (y - 1) > wordCount Or y < 0 Then
    ExtractNthWord = ""
Else
    ExtractNthWord = word(y - 1)
End If
End Function

Run-time error 1004, what can it be?
 
Upvote 0
at what line are you getting the error at?
 
Upvote 0
You will need to have a list of the current sheet names & a list of the "TEST1 (125) MY NAME" values so we know what sheet should be renamed.
Do you have a list like that & if so where is it?
 
Upvote 0
at what line are you getting the error at?

If you put only the part before function, the error is on ExtractNthWord. print screen attached.
If I put the whole thing error 1004, without showing where is the error
 

Attachments

  • ERROR.png
    ERROR.png
    166.6 KB · Views: 8
Upvote 0
that sub procedure is calling the function ExtractNthWord. You need to add that. Take a screenshot of what the error is showing when the function is inserted.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

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