Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default totally newbie to VBA need help


Now I'm reading "Excel VBA Macro Programming". It say that:
Here is a simple example that adds a new menu item under Tools and
attaches some code to it. Insert a module and then add the following
subroutine:

Sub Test_Menu()
MsgBox "You pressed my menu item"
End Sub
When this is run, it will display a message box with the message “You
pressed my menu item.”

Now add the following code. Note that there is a continuation character
(underscore) shown in two of the lines. This allows long lines of code
to wrap around onto the next line but still execute:

Sub MenuCommand()
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls.Add _
(Type:=msoControlButton).Caption = "MyMenu"
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls("MyMenu").OnAction = "Test_Menu"
End Sub
Run the code only once and then go to the spreadsheet. Choose Tools
from the menu, and you will see an option at the bottom called MyMenu.
Select it, and you will get your message box. (If you run this code
again, a second menu item called MyMenu will appear, which could be
confusing.)

The first line of the code adds the menu bar MyMenu to the Tools menu.
The second line of code describes what action to take when the user
does this. The OnAction property is set to point to the subroutine
Test_Menu, which you just created.

Afer typing this code into module and pressing F5, a error message
displays:
"error 5. Invalid procedure call or parameter"
Then the follwing code is pointed and become yellow color:
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls.Add _
(Type:=msoControlButton).Caption = "MyMenu"

Could anyone tell me where thing goes wrong and how to fix it?
many thanks


--
cuongvt
------------------------------------------------------------------------
cuongvt's Profile: http://www.excelforum.com/member.php...o&userid=13439
View this thread: http://www.excelforum.com/showthread...hreadid=471652

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default totally newbie to VBA need help

Hi Cuongvt,

The error you have encountered would occur if the "Worksheet Menu Bar".
commandbar or the "Tools" menu item did not exist. If you were using
non-English version of Excel the 'Tools' menu might well be otherwise named,
for example.

---
Regards,
Norman



"cuongvt" wrote in
message ...

Now I'm reading "Excel VBA Macro Programming". It say that:
Here is a simple example that adds a new menu item under Tools and
attaches some code to it. Insert a module and then add the following
subroutine:

Sub Test_Menu()
MsgBox "You pressed my menu item"
End Sub
When this is run, it will display a message box with the message "You
pressed my menu item."

Now add the following code. Note that there is a continuation character
(underscore) shown in two of the lines. This allows long lines of code
to wrap around onto the next line but still execute:

Sub MenuCommand()
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls.Add _
(Type:=msoControlButton).Caption = "MyMenu"
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls("MyMenu").OnAction = "Test_Menu"
End Sub
Run the code only once and then go to the spreadsheet. Choose Tools
from the menu, and you will see an option at the bottom called MyMenu.
Select it, and you will get your message box. (If you run this code
again, a second menu item called MyMenu will appear, which could be
confusing.)

The first line of the code adds the menu bar MyMenu to the Tools menu.
The second line of code describes what action to take when the user
does this. The OnAction property is set to point to the subroutine
Test_Menu, which you just created.

Afer typing this code into module and pressing F5, a error message
displays:
"error 5. Invalid procedure call or parameter"
Then the follwing code is pointed and become yellow color:
CommandBars("Worksheet Menu Bar").Controls _
("Tools").Controls.Add _
(Type:=msoControlButton).Caption = "MyMenu"

Could anyone tell me where thing goes wrong and how to fix it?
many thanks


--
cuongvt
------------------------------------------------------------------------
cuongvt's Profile:
http://www.excelforum.com/member.php...o&userid=13439
View this thread: http://www.excelforum.com/showthread...hreadid=471652



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
What If, Count - totally confused...... LancsGirl Excel Worksheet Functions 4 February 23rd 09 05:10 PM
Totally confused - please help! James Excel Worksheet Functions 3 April 14th 08 10:23 PM
Help! Totally Grey Sheet nabanco Excel Worksheet Functions 2 August 2nd 07 07:16 PM
Totally wierd Fritz Excel Worksheet Functions 4 July 23rd 07 01:44 PM
Totally Stumped Sondra Excel Discussion (Misc queries) 3 February 15th 06 02:01 PM


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