ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Auto Naming Worksheets (https://www.excelbanter.com/excel-discussion-misc-queries/246373-auto-naming-worksheets.html)

Will Cendrowski[_2_]

Auto Naming Worksheets
 
I have a workbook that has multiple worksheets. I would like to name the
worksheets and used a macro that I found in one of the threads:

Sub name_sheets()
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

The macro works great but their are some A1 cells in some of the sheets that
will not be named until the user completes a master form and the name is
filtered to the corresponding sheet.

Being new to the macro world, how can I have the macro skip the sheets that
have a 'null' value in A1?






Jacob Skaria

Auto Naming Worksheets
 
Sub name_sheets()
For Each ws In Worksheets
If Trim(ws.Range("A1")) < "" ws.Name = ws.Range("A1").Value
Next
End Sub


If this post helps click Yes
---------------
Jacob Skaria


"Will Cendrowski" wrote:

I have a workbook that has multiple worksheets. I would like to name the
worksheets and used a macro that I found in one of the threads:

Sub name_sheets()
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

The macro works great but their are some A1 cells in some of the sheets that
will not be named until the user completes a master form and the name is
filtered to the corresponding sheet.

Being new to the macro world, how can I have the macro skip the sheets that
have a 'null' value in A1?






Mike H

Auto Naming Worksheets
 
Hi,

A null value will generate an error so use this

Sub name_sheets()
On Error Resume Next
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

Mike

"Will Cendrowski" wrote:

I have a workbook that has multiple worksheets. I would like to name the
worksheets and used a macro that I found in one of the threads:

Sub name_sheets()
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

The macro works great but their are some A1 cells in some of the sheets that
will not be named until the user completes a master form and the name is
filtered to the corresponding sheet.

Being new to the macro world, how can I have the macro skip the sheets that
have a 'null' value in A1?






Dave Peterson

Auto Naming Worksheets
 
Check replies at your post from yesterday.

Will Cendrowski wrote:

I have a workbook that has multiple worksheets. I would like to name the
worksheets and used a macro that I found in one of the threads:

Sub name_sheets()
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

The macro works great but their are some A1 cells in some of the sheets that
will not be named until the user completes a master form and the name is
filtered to the corresponding sheet.

Being new to the macro world, how can I have the macro skip the sheets that
have a 'null' value in A1?


--

Dave Peterson

Jacob Skaria

Auto Naming Worksheets
 
You can use the worksheet change event so that the tab name can be renamed as
an when the user change the cell A1 in the sheet.. From workbook press
Alt+F11 to launch VBE (Visual Basic Editor). From the left treeview search
for the workbook name and click on + to expand it. Within that you should see
the following

VBAProject(Your_Filename)
Microsoft Excel Objects
Sheet1(Sheet1)
Sheet2(Sheet2)
Sheet3(Sheet3)
This Workbook

Double click 'This WorkBook' and paste the below code to the right code pane.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Count = 1 And Target.Address = "$A$1" Then
Application.EnableEvents = False
If Trim(Target.Text) < "" Then Sh.Name = Trim(Range("A1"))
Application.EnableEvents = True
End If
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

Sub name_sheets()
For Each ws In Worksheets
If Trim(ws.Range("A1")) < "" ws.Name = ws.Range("A1").Value
Next
End Sub


If this post helps click Yes
---------------
Jacob Skaria


"Will Cendrowski" wrote:

I have a workbook that has multiple worksheets. I would like to name the
worksheets and used a macro that I found in one of the threads:

Sub name_sheets()
For Each ws In Worksheets
ws.Name = ws.Range("A1").Value
Next
End Sub

The macro works great but their are some A1 cells in some of the sheets that
will not be named until the user completes a master form and the name is
filtered to the corresponding sheet.

Being new to the macro world, how can I have the macro skip the sheets that
have a 'null' value in A1?







All times are GMT +1. The time now is 02:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com