LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Alt+F11, writing macros and assigning to a button

[Alt]+[F11], as you've seen, opens the Visual Basic for Applications Editor
(VBE).

Typically you then use Insert -- Module to create an empty code module to
copy and paste code provided here into.

But that's not always the case! There are other places that code can reside:
Class Modules: very rarely used in these forums.

UserForms: again, not used too often in these forums

Worksheet code modules - you can tell when code is intended for a worksheet
because it will normally start with a statement like
Private Sub Worksheet_event() where 'event' is an event that can be
recognized by the worksheet such as Activate, Deactivate, Change,
SelectionChange and BeforeDoubleClick.

The Workbook code module - you can tell when code is intended for a workbook
because it will normally start with a statement like
Private Sub Workbook_event() where 'event' is an event that can be
recognized by the workbook such as _Open, _BeforeClose, _BeforePrint

To get to the code modules for the workbook and worksheets, open the VBA
Project window in the VBE by pressing the [F4] key if it is not visible.
Expand the list of 'Microsoft Excel Objects' in that window. To view the
Workbook's code module, double-click on the 'ThisWorkbook' object in the
list. To view a specific worksheet's code module, double-click on the
desired worksheet name entry.

Now for your problem with the Command button code. If you used the command
button from the Control Toolbox, the code needs to go into the Worksheet code
module for the sheet that the button was placed onto.

If you had used the command button from the Forms toolbar, you could have
right-clicked it and chosen your macro from a list of available macros.

The reason the code you were given did not appear in the list of available
macros is because of the word "Private " at the beginning of its definition.
When a Sub is identified as Private, it does not appear in that list, nor
does it appear in the list of macros using Tools -- Macro -- Macros.

Bottom line: delete the code in the regular code module after copying and
pasting it into the appropriate worksheet code module and things should work
for you. There may already be a code stub for the Command button in that
module with nothing but
Private Sub CommandButton1_Click()

End Sub
in it, just paste your code over that.

Hope this helps.

"CousinExcel" wrote:

Hi,

Recently KevHardy asked a question with a very nice text and
Master Mike H. (who can also inititate chemical reactions in Excel when
needed I believe) has given the solution, effective, easily understandable as
always.
(It was on Feb 11 or 12).

The answer was a macro like:
"Private Sub CommandButton1_Click()
If Selection.Rows.Count 1...

In that answer
- Alt+F11: I learned it. Then should I say insert MODULE or different ?
- Then, I inserted the 'command button' on the worksheet, okay. Right click
the 'command button, select 'Assign Macro' then I could not see the macro I
wrote ? (I could run it by copying, pasting etc., a long way).

Additional question, can I find the list of commands like
"Selection.Rows.Count" etc. somewhere in the internet.

Thanks and regards,

Cousin Excel

 
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
Assigning Macros Randy Excel Worksheet Functions 4 November 15th 07 08:56 PM
Assigning macros Mandy Excel Discussion (Misc queries) 4 February 7th 07 08:43 AM
assigning macros enyaw Excel Discussion (Misc queries) 1 September 6th 06 04:39 PM
Assigning Macros to Buttons Christine Excel Discussion (Misc queries) 1 May 11th 06 09:42 PM
Problem Assigning Macros Rafe Excel Worksheet Functions 2 June 17th 05 06:40 PM


All times are GMT +1. The time now is 11:51 PM.

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"