Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
GLT GLT is offline
external usenet poster
 
Posts: 12
Default Copying worksheets in VBA

Hi,

I used a macro to copy a worksheet and the code that resulted was the
following:

Sheets("Backup Listing").Copy Befo=Sheets(1)

The result is a worksheet with the same name but the number 2 in brackets.

Does anyone know if I can force this worksheet to have a name (say SHEET2)
in the command above - rather than Excel allocating a name?

Also, when I open the workbook, I need to delete any copies of (SHEET2) that
may exist.

If anyone could provide any assistance I would be most greatful.

Cheers,
GLT.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Copying worksheets in VBA

Try this

Sub test()
Sheets("Backup Listing").Copy Befo=Sheets(1)
On Error Resume Next
ActiveSheet.Name = "Sheet2"
If Err.Number 0 Then
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
ActiveSheet.Name = "Sheet2"
Application.DisplayAlerts = True
Err.Clear
Else
ActiveSheet.Name = "Sheet2"
End If
On Error GoTo 0
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"GLT" wrote in message ...
Hi,

I used a macro to copy a worksheet and the code that resulted was the
following:

Sheets("Backup Listing").Copy Befo=Sheets(1)

The result is a worksheet with the same name but the number 2 in brackets.

Does anyone know if I can force this worksheet to have a name (say SHEET2)
in the command above - rather than Excel allocating a name?

Also, when I open the workbook, I need to delete any copies of (SHEET2) that
may exist.

If anyone could provide any assistance I would be most greatful.

Cheers,
GLT.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Copying worksheets in VBA

Why not just delete it to start:

Sub test()

On Error Resume Next
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
Application.DisplayAlerts = True
On error goto 0

Sheets("Backup Listing").Copy Befo=Sheets(1)
ActiveSheet.Name = "Sheet2"

End Sub

Ron de Bruin wrote:

Try this

Sub test()
Sheets("Backup Listing").Copy Befo=Sheets(1)
On Error Resume Next
ActiveSheet.Name = "Sheet2"
If Err.Number 0 Then
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
ActiveSheet.Name = "Sheet2"
Application.DisplayAlerts = True
Err.Clear
Else
ActiveSheet.Name = "Sheet2"
End If
On Error GoTo 0
End Sub

--
Regards Ron de Bruin
http://www.rondebruin.nl

"GLT" wrote in message ...
Hi,

I used a macro to copy a worksheet and the code that resulted was the
following:

Sheets("Backup Listing").Copy Befo=Sheets(1)

The result is a worksheet with the same name but the number 2 in brackets.

Does anyone know if I can force this worksheet to have a name (say SHEET2)
in the command above - rather than Excel allocating a name?

Also, when I open the workbook, I need to delete any copies of (SHEET2) that
may exist.

If anyone could provide any assistance I would be most greatful.

Cheers,
GLT.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Copying worksheets in VBA

Yes this is better Dave

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Dave Peterson" wrote in message ...
Why not just delete it to start:

Sub test()

On Error Resume Next
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
Application.DisplayAlerts = True
On error goto 0

Sheets("Backup Listing").Copy Befo=Sheets(1)
ActiveSheet.Name = "Sheet2"

End Sub

Ron de Bruin wrote:

Try this

Sub test()
Sheets("Backup Listing").Copy Befo=Sheets(1)
On Error Resume Next
ActiveSheet.Name = "Sheet2"
If Err.Number 0 Then
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
ActiveSheet.Name = "Sheet2"
Application.DisplayAlerts = True
Err.Clear
Else
ActiveSheet.Name = "Sheet2"
End If
On Error GoTo 0
End Sub

--
Regards Ron de Bruin
http://www.rondebruin.nl

"GLT" wrote in message ...
Hi,

I used a macro to copy a worksheet and the code that resulted was the
following:

Sheets("Backup Listing").Copy Befo=Sheets(1)

The result is a worksheet with the same name but the number 2 in brackets.

Does anyone know if I can force this worksheet to have a name (say SHEET2)
in the command above - rather than Excel allocating a name?

Also, when I open the workbook, I need to delete any copies of (SHEET2) that
may exist.

If anyone could provide any assistance I would be most greatful.

Cheers,
GLT.


--

Dave Peterson



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
Copying Worksheets dpridemore Excel Discussion (Misc queries) 3 October 9th 08 12:48 AM
Copying between Worksheets LongTermNoob Excel Worksheet Functions 5 January 3rd 08 07:00 PM
Copying Worksheets babygoode Excel Worksheet Functions 0 July 27th 06 11:18 PM
copying worksheets into one Carolyn Excel Worksheet Functions 0 May 7th 06 05:31 PM
Copying worksheets gingerly88 Excel Worksheet Functions 2 August 1st 05 06:58 PM


All times are GMT +1. The time now is 03:54 PM.

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"