Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Worksheet copy with rename

Private Sub CommandButton1_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
Set sht2 = Worksheets.Add(after:=sht1)
sht2.Name = sht1.Range("J1").Value
sht1.UsedRange.Copy
sht2.Range("A1").PasteSpecial (xlPasteAll)


End Sub

The above code is supposed to:
1. create a new worksheet after the current worksheet containing all the information in the current worksheet and
2. To renme the worksheet to the contents of J1 in the first worksheet.

The problem:
Depending on the order of the statements, it either creates the new worksheet and renames it correctly OR it creates the worksheet
and correctly copies the info. I can't get it to do both.

What am I doing wrong? Is there a better way? Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Worksheet copy with rename

It all seems good, and it works fine for me.

You don't have protection or anything else unusual?

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Doug Broad" wrote in message
...
Private Sub CommandButton1_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
Set sht2 = Worksheets.Add(after:=sht1)
sht2.Name = sht1.Range("J1").Value
sht1.UsedRange.Copy
sht2.Range("A1").PasteSpecial (xlPasteAll)


End Sub

The above code is supposed to:
1. create a new worksheet after the current worksheet containing all

the information in the current worksheet and
2. To renme the worksheet to the contents of J1 in the first

worksheet.

The problem:
Depending on the order of the statements, it either creates the new

worksheet and renames it correctly OR it creates the worksheet
and correctly copies the info. I can't get it to do both.

What am I doing wrong? Is there a better way? Thanks




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default Worksheet copy with rename

Worksheets.Add adds a new, blank worksheet
Worksheet(sht1).Copy will create a copy of sht1

Maybe something like:
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
sht1.Copy After:=sht1
Set sht2 = Worksheets(sht1.index + 1)
sht2.Name = sht1.Range("J1").Value

Otherwise, if you really want to use PasteSpecial, explicitly activate sht2
just before PasteSpecial. The Help entry for PasteSpecial says: "You must
select the destination before using this method." I haven't found that to be
entirely true, but you *do* have to make sure that at least the destination
*sheet* is Activated or Selected (one of the few times either are actually
necessary). sht2 *should* automatically be active right after the Add, but
better to be explicit if things aren't working as you expect. I believe the
following should work:

sht1.UsedRange.Copy
sht2.Activate
sht2.Range("A1").PasteSpecial (xlPasteAll)

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Doug Broad" wrote in message
...
Private Sub CommandButton1_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
Set sht2 = Worksheets.Add(after:=sht1)
sht2.Name = sht1.Range("J1").Value
sht1.UsedRange.Copy
sht2.Range("A1").PasteSpecial (xlPasteAll)


End Sub

The above code is supposed to:
1. create a new worksheet after the current worksheet containing all
the information in the current worksheet and
2. To renme the worksheet to the contents of J1 in the first
worksheet.

The problem:
Depending on the order of the statements, it either creates the new
worksheet and renames it correctly OR it creates the worksheet
and correctly copies the info. I can't get it to do both.

What am I doing wrong? Is there a better way? Thanks




  #4   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Worksheet copy with rename

Hi Doug,

You could do this a couple different ways, depending on whether you want a
copy of the first sheet, or if you want a new sheet with the data from the
first sheet copied onto it.

In either case, the sheet is renamed how you wanted. Here's a couple you can
try:


Sub CopyAndName()
' This creates a new sheet,
' copies the data from the first sheet,
' and renames it to a specified cell's value.

Dim sName As String, sht1 As Worksheet, sht2 As Worksheet

Set sht1 = ActiveSheet
sName = sht1.Range("A1").Value 'change the reference to your location
Set sht2 = Worksheets.Add(after:=ActiveSheet)
sht2.Name = sName
sht1.UsedRange.Copy Destination:=sht2.Range("A1")

End Sub



Sub CopyAndName2()
' This makes a copy of the ActiveSheet,
' and renames it to a specified cell's value

ActiveSheet.Copy after:=ActiveSheet

'At this point, it's the ActiveSheet,
'and contains the cell holding its name.
ActiveSheet.Name = Range("A1").Value 'change the reference to your
location

End Sub

Regards,
GS
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Worksheet copy with rename

Thanks George. Must be something with my computer or version of Excel because
I can run all these programs at home and get good results but on this computer
yours copies the sheet but doesn't rename.

Appreciate your time.

"George Nicholson" wrote in message ...
Worksheets.Add adds a new, blank worksheet
Worksheet(sht1).Copy will create a copy of sht1

Maybe something like:
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
sht1.Copy After:=sht1
Set sht2 = Worksheets(sht1.index + 1)
sht2.Name = sht1.Range("J1").Value

Otherwise, if you really want to use PasteSpecial, explicitly activate sht2
just before PasteSpecial. The Help entry for PasteSpecial says: "You must
select the destination before using this method." I haven't found that to be
entirely true, but you *do* have to make sure that at least the destination
*sheet* is Activated or Selected (one of the few times either are actually
necessary). sht2 *should* automatically be active right after the Add, but
better to be explicit if things aren't working as you expect. I believe the
following should work:

sht1.UsedRange.Copy
sht2.Activate
sht2.Range("A1").PasteSpecial (xlPasteAll)

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Doug Broad" wrote in message
...
Private Sub CommandButton1_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
Set sht2 = Worksheets.Add(after:=sht1)
sht2.Name = sht1.Range("J1").Value
sht1.UsedRange.Copy
sht2.Range("A1").PasteSpecial (xlPasteAll)


End Sub

The above code is supposed to:
1. create a new worksheet after the current worksheet containing all
the information in the current worksheet and
2. To renme the worksheet to the contents of J1 in the first
worksheet.

The problem:
Depending on the order of the statements, it either creates the new
worksheet and renames it correctly OR it creates the worksheet
and correctly copies the info. I can't get it to do both.

What am I doing wrong? Is there a better way? Thanks








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Worksheet copy with rename

Hi George,
Thanks. At home both your functions and both mine work. Here,
your first one works fine and your second one does not rename.
Have tried it on a number of worksheets, none protected.
Thanks again. Still curious why it works on one machine and not
the other.

Thanks for your time.


"GS" wrote in message ...
Hi Doug,

You could do this a couple different ways, depending on whether you want a
copy of the first sheet, or if you want a new sheet with the data from the
first sheet copied onto it.

In either case, the sheet is renamed how you wanted. Here's a couple you can
try:


Sub CopyAndName()
' This creates a new sheet,
' copies the data from the first sheet,
' and renames it to a specified cell's value.

Dim sName As String, sht1 As Worksheet, sht2 As Worksheet

Set sht1 = ActiveSheet
sName = sht1.Range("A1").Value 'change the reference to your location
Set sht2 = Worksheets.Add(after:=ActiveSheet)
sht2.Name = sName
sht1.UsedRange.Copy Destination:=sht2.Range("A1")

End Sub



Sub CopyAndName2()
' This makes a copy of the ActiveSheet,
' and renames it to a specified cell's value

ActiveSheet.Copy after:=ActiveSheet

'At this point, it's the ActiveSheet,
'and contains the cell holding its name.
ActiveSheet.Name = Range("A1").Value 'change the reference to your
location

End Sub

Regards,
GS



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Worksheet copy with rename

Bob,
I don't know what's going on. On one computer, both of my versions work fine.
Here, it remains as I explained. GS's solution 1 does work but I am not sure why.
Thanks for suggesting I check for protection. No protection was on the sheet or
the workbook. Still wondering but have a working program. Must test on a few
other machines to be sure.

Thanks again.


"Bob Phillips" wrote in message ...
It all seems good, and it works fine for me.

You don't have protection or anything else unusual?

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Doug Broad" wrote in message
...
Private Sub CommandButton1_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ActiveSheet
Set sht2 = Worksheets.Add(after:=sht1)
sht2.Name = sht1.Range("J1").Value
sht1.UsedRange.Copy
sht2.Range("A1").PasteSpecial (xlPasteAll)


End Sub

The above code is supposed to:
1. create a new worksheet after the current worksheet containing all

the information in the current worksheet and
2. To renme the worksheet to the contents of J1 in the first

worksheet.

The problem:
Depending on the order of the statements, it either creates the new

worksheet and renames it correctly OR it creates the worksheet
and correctly copies the info. I can't get it to do both.

What am I doing wrong? Is there a better way? Thanks






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
Macro: Copy a worksheet and rename it to a specified value mitch Excel Discussion (Misc queries) 2 February 16th 09 05:43 PM
Copy worksheet and rename sheet Ernst - EXE Graphics Excel Discussion (Misc queries) 4 July 25th 08 01:18 PM
Copy/Paste Worksheet to last and rename with input box for value. bigwheel[_2_] Excel Programming 0 September 7th 04 10:17 PM
Copy/Paste Worksheet to last and rename with input box for value. Dave Peterson[_3_] Excel Programming 2 September 7th 04 08:59 PM
Copy worksheet from multiple files in one DIR to another DIR & rename Mike Taylor Excel Programming 1 July 13th 03 03:28 PM


All times are GMT +1. The time now is 02:26 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"