Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2003 - rename worksheet using date and time 1

Status
Not open for further replies.

smooveb

IS-IT--Management
Jul 20, 2001
161
US
Hi,

I'm very close to being able to insert a copy of one worksheet at the end of my workbook, and rename the new sheet using current date and time. Date is easy, it's time that's giving me a little hassle - presumably since Excel doesn't like you using : in your sheetname. Here's my code:

Dim curdate As String
curdate = Format(Date, "mm-dd-yy hh:mm:ss")
Sheets("Summary").Select
ActiveSheet.Cells.Select
Selection.Copy
Dim nLast As Long
Dim i As Long

With ActiveWorkbook
nLast = .Sheets.Count
For i = 1 To ActiveWindow.SelectedSheets.Count
.Sheets.Add _
After:=.Sheets(.Sheets.Count), _
Type:="worksheet", _
Count:=1
Next i
.Sheets(nLast + 1).Select
ActiveCell.Cells.Select
ActiveSheet.Paste
ActiveSheet.Name = curdate

End With

Only the activesheet.name = curdate is giving me hassles. Any recommendations how to scrub the colons before this line?

Thanks!
Barrett
 



If you would have tried renaming a sheet tab MANUALLY, you would have observed that the COLON is not accepted as a valid character in a sheet name.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sorry, I thought my post indicated that I had tried this, and appreciate the need to scrub the colons from curdate before trying to name the sheet with it.

To clear up the question, I'm trying to set curdate to reflect date and time, but without colons.

Thanks!
Barrett
 


using what code?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


why not
Code:
curdate = Format(Date, "mm-dd-yy hhmmss")

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi,

perhaps I'm still not being clear.

I'm using the code:

curdate = Format(Date, "mm-dd-yy hh:mm:ss")

to set curdate. I need to use alternative code to set curdate to include month, day, year, hours, minutes and seconds, but without the colons (as we've discussed, a worksheet cannot be named using colons).

I'm here to ask for ideas for alternate code to use that will meet my needs.

Cheers,
Barrett
 


Check my previous post.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Yup, that worked. Part of what I was doing wrong was setting only using date, but not time as well. There may be a better way of doing this, but it worked. Thanks!


Dim curdate As String
curdate = Format(Date, "mmmddyy") & Format(Time, "hhmmss")
Sheets("Summary").Select
ActiveSheet.Cells.Select
Selection.Copy
Dim nLast As Long
Dim i As Long

With ActiveWorkbook
nLast = .Sheets.Count
For i = 1 To ActiveWindow.SelectedSheets.Count
.Sheets.Add _
After:=.Sheets(.Sheets.Count), _
Type:="worksheet", _
Count:=1
Next i
.Sheets(nLast + 1).Select
ActiveCell.Cells.Select
ActiveSheet.Paste
ActiveSheet.Name = curdate

End With



End Sub
 



consider...
Code:
    Dim nLast As Long
    Dim i As Long
    
    Sheets("Summary").Cells.Copy
        
    With ActiveWorkbook
        nLast = .Sheets.Count
        For i = 1 To ActiveWindow.SelectedSheets.Count
            .Sheets.Add _
                    After:=.Sheets(.Sheets.Count), _
                    Type:="worksheet", _
                    Count:=1
        Next i
        With .Sheets(nLast + 1)
            .[A1].Paste
            .Name = [b]Format(Now, "mmmddyy hhmmss")[/b]
        End With
    End With
No need to create a variable for curdate, as you only use it one time.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
No need to create a variable for curdate, as you only use it one time. "

yeah hurray!

Gerry
 
just curious about the selecting and copying and looping etc. would this work as well?

Code:
Sub mit()
Worksheets("Sheet1").Copy after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = Format(Now, "yymmdd hhmmss")
End Sub

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 


Yes, this was a curiosity to me. You have this loop going on, adding sheets, and then ONLY the last sheet is named. Shouldn't the naming be going on INSIDE the loop?
Code:
    With ActiveWorkbook
        For i = 1 To ActiveWindow.SelectedSheets.Count
            .Sheets.Add _
                    After:=.Sheets(.Sheets.Count), _
                    Type:="worksheet", _
                    Count:=1[b]
            With .Sheets(.Sheets.Count)
                .[A1].Paste
                .Name = Format(Now, "mmmddyy hhmmss") & i
            End With[/b]
        Next i
    End With

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top