LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default potential bug in copy method

The worksheet.copy function returns a boolean success, not a worksheet
object. Try something like this -

Sub test2()
Dim wsSource As Worksheet
Dim wsNew As Worksheet

With ActiveWorkbook
Set wsSource = .Worksheets("Sheet1")
wsSource.Copy after:=wsSource
Set wsNew = .Worksheets(wsSource.Index + 1)
wsNew.Name = "NewSheet"
End With
End Sub

However, normally you can do simply
Set wsNew = ActiveSheet
even if the workbook wasn't active originally

Regards,
Peter T

"whburling" wrote in message
...
My goal is to create a copy of a worksheet and place it within the
same workbook. I chose the copy method as
my approach. furthermore I sought to be a bit more elegant
and hence created a worksheet variable in the same line of code
through the use of the set statement:

Sub test()
Dim aworkbook as workbook
Dim newworksheet as worksheet

with workbooks("aworkbook.xls")
set newworksheet= .worksheets("aworksheet").copy (_
after:=.worksheets("aworksheet"))
newworksheet.name = "test"
end with
end sub

The above code fails upon completing the copy command;
the code DOES copy the worksheet, "aworksheet" and DOES
place it immediately after the original with the name, "aworksheet
(2)" However, the code DOES NOT define the worksheet variable,
newworksheet, with the set method. I get the error message:
'424' Object required.

NOW.....in playing with variations of the above code, I found that
if I replaced the "copy" method with an "add" method, the program at
least executed without error even if I never achieved
my goal of copying. ie:

Sub test()
Dim aworkbook as workbook
Dim newworksheet as worksheet

with workbooks("aworkbook.xls")
set newworksheet= .worksheets.add (_
after:=.worksheets("aworksheet"))
newworksheet.name = "test" 'worked; worksheet name became
"test"
end with
end sub

To me, the fact that the add method did not produce a run-time error
with the "set" command and the copy method did produce a run-time
error suggests there is a bug in the copy method -
that is the copy method does not correctly return an address to the
newly created worksheet object that will become the address associated
with newworksheet.

Does any one else have any insight into why the add method works
without a run-time error but the copy method produces a run-time
error?



 
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
potential duplicate max values Caren Excel Worksheet Functions 2 February 12th 09 04:04 PM
Potential Bug in Find All application MP Excel Discussion (Misc queries) 1 November 25th 08 12:34 PM
Potential CSV/Excel Conversion Issues? Erin Excel Discussion (Misc queries) 2 May 15th 06 10:30 PM
Formula How to add potential nos Wilie Loh Excel Worksheet Functions 2 March 19th 06 02:48 PM
Why QUIT method doesn't work after COPY method? surotkin Excel Programming 3 October 26th 05 04:32 PM


All times are GMT +1. The time now is 10:39 AM.

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

About Us

"It's about Microsoft Excel"