Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As part of a series of macros, I name a new sheet that I open with the
current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() -----Original Message----- As part of a series of macros, I name a new sheet that I open with the current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance . Try this. Right before the rename of the sheet, put in the code: On Error Resume Next then... your rename code then... If Err 0 then Sheet("whatever").name = "newnameappendage" Endif then On Error Go To 0 The first On Error will allow the rename command to NOT fail hard, and let you evaluate the error and do something....give it another name....The on error go to 0 turns back on error checking so that future errors will be caught. hope this helps....the go to might need to be goto instead..can't remember. The 'then's' on the left margin are NOT code...just my comments Steve |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'll give 2 ways:
1. This will add a sheet with today's name if none exists and do nothing if one does. You can refer to it prior to appending any data to today's sheet Sub name_sheet_as_today() dup = "no" For Each sheet In Sheets() If sheet.Name = date$ Then dup = "yes" Next If dup = "no" Then Sheets.Add.name=date$ End If End Sub Later, you can activate this page by inserting: sheets(date$).activate or sheets(date$).select in your appropriate routine. A second subroutine that is much shorter: Sub name_with_error() On Error Resume Next Sheets.Add.Name = Date$ End Sub *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You could integrate Chip Pearson's function to test if a sheet exists...
Function WorksheetExists(WSName As String, Optional WB As Workbook = Nothing) As Boolean On Error Resume Next WorksheetExists = CBool(Len(IIf(WB Is Nothing, ThisWorkbook, WB).Worksheets(WSName).Name)) End Function Sub Tester() If WorksheetExists(Date$) = True Then ActiveSheet.Name = Date$ & "A" Else ActiveSheet.Name = Date$ End If End Sub -- Regards, Rocky McKinley "Mark R" wrote in message ... As part of a series of macros, I name a new sheet that I open with the current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Error Resume Next
ActiveSheet.Name = Date$ On Error GoTo 0 Or more sopisticated: Sub NameAsDate() Dim s As String s = Date$ If Not WorksheetExists(s) Then ActiveSheet.Name = s Else Dim i As Integer Do Until Not WorksheetExists(s) s = Date$ & Chr(65 + i) i = i + 1 Loop ActiveSheet.Name = s End If End Sub Function WorksheetExists(wsName As String, _ Optional wbName As String) As Boolean If wbName = "" Then wbName = _ ActiveWorkbook.Name On Error Resume Next WorksheetExists = CBool(Len(Workbooks(wbName) _ .Worksheets(wsName).Name)) End Function -- Vasant "Mark R" wrote in message ... As part of a series of macros, I name a new sheet that I open with the current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks so much. This worked perfectly.
"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... On Error Resume Next ActiveSheet.Name = Date$ On Error GoTo 0 Or more sopisticated: Sub NameAsDate() Dim s As String s = Date$ If Not WorksheetExists(s) Then ActiveSheet.Name = s Else Dim i As Integer Do Until Not WorksheetExists(s) s = Date$ & Chr(65 + i) i = i + 1 Loop ActiveSheet.Name = s End If End Sub Function WorksheetExists(wsName As String, _ Optional wbName As String) As Boolean If wbName = "" Then wbName = _ ActiveWorkbook.Name On Error Resume Next WorksheetExists = CBool(Len(Workbooks(wbName) _ .Worksheets(wsName).Name)) End Function -- Vasant "Mark R" wrote in message ... As part of a series of macros, I name a new sheet that I open with the current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Wow, thanks to all!
"Mark R" wrote in message ... As part of a series of macros, I name a new sheet that I open with the current date with ActiveSheet.Name = Date$ Occasionally I need to run this routine more than once in the same day, which gives an error Run time error "1004": Cannot rename a sheet to the same name as another sheet . . ." Is there a way to check if there is a sheet with the current date name, and then name the new sheet with an appendage - for example 12-21-2003A, 12-21-2003B etc. I know very little VBA, so as specific of code as possible would be greatly appreciated. If that's unreasonably difficult, is there is a way to allow the macros to continue past the error, without renaming the sheet in that case? Thanks in advance |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Naming a sheet | Excel Discussion (Misc queries) | |||
Naming Sheet | Excel Discussion (Misc queries) | |||
Chart Axis naming question | Charts and Charting in Excel | |||
Question about tab naming and referenced cells | Excel Discussion (Misc queries) | |||
Yet another question naming ranges | Excel Programming |