Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
O'C O'C is offline
external usenet poster
 
Posts: 4
Default Worksheet naming using visual basic

What I have is a work schedule based on a calendar setup. I currently use a
macro to automatically save the "schedule" worksheet by copying the worksheet
to a new sheet which renames it using the date from cell A2, i.e. "26 Oct -
15 Nov 2006".

The code looks like this:

Sheets("SCHEDULE").Copy AFTER:=Worksheets(Worksheets.Count)

Sheets("SCHEDULE (2)").Select
Cells.Select
Selection.Copy
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("a1").Select

Sheets("SCHEDULE (2)").Name = Sheets("SCHEDULE (2)").Range("$a$2")

Sheets("INFO").Select
Calculate

Sheets("SCHEDULE").Select
Range("A1").Select

The problem I have is that when I make a mistake and/or correction and try
to save it again I get the old run time error 1004 saying that you can't
rename a sheet when I have that same existing name from the previous save.
When I stop the debugger it still copies it to "schedule (2)".

Here's what I want. I want it to either overwrite the same named file with
the new one or automatically name it with the date and the (2) symbol and so
on. I don't want the debugger to be tripped by an error.

I appreciate any help you can give me...

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 10,593
Default Worksheet naming using visual basic

Dim ws As Worksheet
Dim i As Long
Dim wsTemp As Worksheet
Dim sName As String

Sheets("SCHEDULE").Copy AFTER:=Worksheets(Worksheets.Count)
Set ws = ActiveSheet
With ws
.UsedRange.Value = .UsedRange.Value
.Range("A1").Select

i = 1
sName = Sheets("SCHEDULE (2)").Range("$A$2").Value
On Error Resume Next
Do
Set wsTemp = Nothing
Set wsTemp = Worksheets(sName & IIf(i = 1, "", "(" & i & ")"))
If wsTemp Is Nothing Then
.Name = sName & IIf(i = 1, "", "(" & i & ")")
Else
i = i + 1
End If
Loop Until wsTemp Is Nothing
On Error GoTo 0
End With

Sheets("INFO").Calculate

Sheets("SCHEDULE").Select
Range("A1").Select


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"O'C" wrote in message
...
What I have is a work schedule based on a calendar setup. I currently use

a
macro to automatically save the "schedule" worksheet by copying the

worksheet
to a new sheet which renames it using the date from cell A2, i.e. "26

Oct -
15 Nov 2006".

The code looks like this:

Sheets("SCHEDULE").Copy AFTER:=Worksheets(Worksheets.Count)

Sheets("SCHEDULE (2)").Select
Cells.Select
Selection.Copy
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("a1").Select

Sheets("SCHEDULE (2)").Name = Sheets("SCHEDULE (2)").Range("$a$2")

Sheets("INFO").Select
Calculate

Sheets("SCHEDULE").Select
Range("A1").Select

The problem I have is that when I make a mistake and/or correction and try
to save it again I get the old run time error 1004 saying that you can't
rename a sheet when I have that same existing name from the previous save.
When I stop the debugger it still copies it to "schedule (2)".

Here's what I want. I want it to either overwrite the same named file

with
the new one or automatically name it with the date and the (2) symbol and

so
on. I don't want the debugger to be tripped by an error.

I appreciate any help you can give me...



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
O'C O'C is offline
external usenet poster
 
Posts: 4
Default Worksheet naming using visual basic

Thanks Bob, it works like a champ. I wish I knew as much about visual basic.
It looks like you can do anything with it.

"Bob Phillips" wrote:

Dim ws As Worksheet
Dim i As Long
Dim wsTemp As Worksheet
Dim sName As String

Sheets("SCHEDULE").Copy AFTER:=Worksheets(Worksheets.Count)
Set ws = ActiveSheet
With ws
.UsedRange.Value = .UsedRange.Value
.Range("A1").Select

i = 1
sName = Sheets("SCHEDULE (2)").Range("$A$2").Value
On Error Resume Next
Do
Set wsTemp = Nothing
Set wsTemp = Worksheets(sName & IIf(i = 1, "", "(" & i & ")"))
If wsTemp Is Nothing Then
.Name = sName & IIf(i = 1, "", "(" & i & ")")
Else
i = i + 1
End If
Loop Until wsTemp Is Nothing
On Error GoTo 0
End With

Sheets("INFO").Calculate

Sheets("SCHEDULE").Select
Range("A1").Select


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"O'C" wrote in message
...
What I have is a work schedule based on a calendar setup. I currently use

a
macro to automatically save the "schedule" worksheet by copying the

worksheet
to a new sheet which renames it using the date from cell A2, i.e. "26

Oct -
15 Nov 2006".

The code looks like this:

Sheets("SCHEDULE").Copy AFTER:=Worksheets(Worksheets.Count)

Sheets("SCHEDULE (2)").Select
Cells.Select
Selection.Copy
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("a1").Select

Sheets("SCHEDULE (2)").Name = Sheets("SCHEDULE (2)").Range("$a$2")

Sheets("INFO").Select
Calculate

Sheets("SCHEDULE").Select
Range("A1").Select

The problem I have is that when I make a mistake and/or correction and try
to save it again I get the old run time error 1004 saying that you can't
rename a sheet when I have that same existing name from the previous save.
When I stop the debugger it still copies it to "schedule (2)".

Here's what I want. I want it to either overwrite the same named file

with
the new one or automatically name it with the date and the (2) symbol and

so
on. I don't want the debugger to be tripped by an error.

I appreciate any help you can give me...




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual basic editor Answerfactory Excel Discussion (Misc queries) 3 October 9th 06 09:13 PM
Adding a dymanic formula to a cell using visual basic Ian Web Excel Discussion (Misc queries) 2 September 28th 06 09:59 PM
Adding a dymanic formula to a cell using visual basic Ian Web Excel Discussion (Misc queries) 0 September 28th 06 09:26 PM
Microsoft Visual Basic Error in Excel mack Excel Discussion (Misc queries) 0 August 24th 06 07:27 PM
Visual basic Check Box and the IF formula dingo33 Excel Discussion (Misc queries) 3 February 15th 06 04:57 PM


All times are GMT +1. The time now is 11:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"