Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default question on excel 2007

I am going through some example vba procedures in a book. The book is
copyrighted 2006. I'm assuming it is because I'm using excel 2007 but this
procedure doesn't work. It says "invalid use of New keyword". Can you tell
me how to fix it?

Sub TestSheetClass()
Dim obj1 As New Sheet1, obj2 As New Sheet1
obj1.Name = "New name"
Debug.Print obj2.Name
' Reset name back to original.
obj2.Name = "Start here"
End Sub

The point of this example is there can only be one name for an object but
now I wonder why it doesn't create the new object.

tia,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default question on excel 2007

I'm not sure whether this is what you want to do. You must use New against a
class name, either built-in or in your VBA project as a Class Module.

Sub TestSheetClass()
Dim obj1 As New Excel.Worksheet, obj2 As New Excel.Worksheet
obj1.Name = "New name"
Debug.Print obj2.Name
' Reset name back to original.
obj2.Name = "Start here"
End Sub

"Janis" wrote:

I am going through some example vba procedures in a book. The book is
copyrighted 2006. I'm assuming it is because I'm using excel 2007 but this
procedure doesn't work. It says "invalid use of New keyword". Can you tell
me how to fix it?

Sub TestSheetClass()
Dim obj1 As New Sheet1, obj2 As New Sheet1
obj1.Name = "New name"
Debug.Print obj2.Name
' Reset name back to original.
obj2.Name = "Start here"
End Sub

The point of this example is there can only be one name for an object but
now I wonder why it doesn't create the new object.

tia,

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default question on excel 2007

Hi,
It's not an xl2007 error.
You cannot create new instance of Sheet1 (Dim obj1 As New Sheet1).
You can however create two variables pointing to the one and single instance
Sheet1

Dim obj1 as worksheet , obj2 as worksheet
Set obj1=Sheet1
Set obj2 = Sheet1
obj1.name = "New name"
debug.print obj2.name ''' same as obj1.name
''' reset
obj2.name="Start here"

You could also do
Dim obj1 as Sheet1, obj2 as Sheet1
but the only thing you say is that obj1 and obj2 are variables of type Class
Sheet1 (like the template of sheet1, not the object itself)
So you still need to do
Set obj1 = Sheet1 ''' assign to the object/instance Sheet1 (not the
class)
Set obj2=sheet1
Now, obj1 and obj2 points to the same object.

--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"Janis" wrote:

I am going through some example vba procedures in a book. The book is
copyrighted 2006. I'm assuming it is because I'm using excel 2007 but this
procedure doesn't work. It says "invalid use of New keyword". Can you tell
me how to fix it?

Sub TestSheetClass()
Dim obj1 As New Sheet1, obj2 As New Sheet1
obj1.Name = "New name"
Debug.Print obj2.Name
' Reset name back to original.
obj2.Name = "Start here"
End Sub

The point of this example is there can only be one name for an object but
now I wonder why it doesn't create the new object.

tia,

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
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
2007 Excel Tab Question WSR Excel Discussion (Misc queries) 0 August 12th 08 01:22 PM
Excel 2007 question bwiii New Users to Excel 1 July 15th 08 02:40 PM
Two question about Excel 2007 MZ Excel Worksheet Functions 4 February 28th 08 12:01 AM
Excel 2007 vba question Kaak[_72_] Excel Programming 1 June 28th 06 07:50 AM


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