Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Run time error '9' Subscript out of range

Hi,
I am getting an error in the following code at the line marked with
****. Any ideas why??
-----------------------

Workbooks("Game").Sheets("Data").Activate
Dim OLEObj As OLEObject
For i = 0 To 4
Set OLEObj =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1",
Link:=False _
, DisplayAsIcon:=False, Left:=50 + i * 100, Top:=10,
Width:=80, Height:= _
25)
OLEObj.name = "TheButton" & i
'
' Create the event procedure
'
With ActiveWorkbook.VBProject.VBComponents(OLEObj.Paren t.name).CodeModule
**** error here

.InsertLines .CreateEventProc("Click", OLEObj.name) + 1,
"Msgbox ""You Clicked The Button"" "

End With

Next i
----------------
TIA!
Tina
  #2   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Run time error '9' Subscript out of range

Tina,

Your error is occuring because the worksheet has been renamed. VBA doesn't
see worksheet names when looking in the VBComponents collection. Instead, it
sees worksheets as named with their CodeName. Change

With

ActiveWorkbook.VBProject.VBComponents(OLEObj.Paren t.name).CodeModule

to
With ActiveWorkbook.VBProject.VBComponents _
(OLEObj.Parent.CodeName).CodeModule

Initially, the Name and CodeName of a worksheet are the same, but if you
rename the sheet, the Name changes but the CodeName remains the same.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com




"Tina" wrote in message
om...
Hi,
I am getting an error in the following code at the line marked with
****. Any ideas why??
-----------------------

Workbooks("Game").Sheets("Data").Activate
Dim OLEObj As OLEObject
For i = 0 To 4
Set OLEObj =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1",
Link:=False _
, DisplayAsIcon:=False, Left:=50 + i * 100, Top:=10,
Width:=80, Height:= _
25)
OLEObj.name = "TheButton" & i
'
' Create the event procedure
'
With

ActiveWorkbook.VBProject.VBComponents(OLEObj.Paren t.name).CodeModule
**** error here

.InsertLines .CreateEventProc("Click", OLEObj.name) + 1,
"Msgbox ""You Clicked The Button"" "

End With

Next i
----------------
TIA!
Tina



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
run-time error '9': subscript out of range [email protected] uk Excel Discussion (Misc queries) 4 December 8th 09 10:27 PM
Error:Subscript out of range Jay Excel Discussion (Misc queries) 1 April 10th 08 10:25 PM
Run Time Error 9 (Subscript out of Range) for XLA file ExcelMonkey Excel Discussion (Misc queries) 3 October 5th 05 03:34 PM
Run time error 9 : Subscript out of range JAtz_DA_WAY Excel Discussion (Misc queries) 6 August 29th 05 08:26 PM
Run time error 9 (subscript out of range) Nathaniel Tigere Excel Programming 2 August 5th 03 11:12 AM


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