Excel assigning a macro to an activex command button from Mr Excel


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

assigning a macro to an activex command button
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

PEACHY
Board Regular


Joined: 26 Nov 2002
Posts: 7


Status: Offline

 Reply with quote  

assigning a macro to an activex command button

Help! Attempting to get a command button to run a simple pre-recorded macro. Zero experience of visual basic , office assistant telling me to type in the macro!!??!! Hello , what. Please help me or shoot me.Brain hurting.

Post Sat Feb 14, 2004 10:45 am 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8319

Flag: Uk

Status: Offline

 Reply with quote  

Re: assigning a macro to an activex command button

If you CommandButton is from the Control ToolBox, right click it and choose View Code. You should something like this:

code:

Private Sub CommandButton1_Click()

End Sub




You can copy your recorded code into there (excluding its Sub and End Sub lines) or run the existing Macro like this:

code:

Private Sub CommandButton1_Click()
   Call Macro1
End Sub




where Macro1 is the name of your recorded Macro.

Post Sat Feb 14, 2004 11:08 am 
 View user's profile Send private message

Richie(UK)
MrExcel MVP


Joined: 18 May 2002
Posts: 2486
Location: Worcester, England
Flag: Uk

Status: Offline

 Reply with quote  

Re: assigning a macro to an activex command button

quote:
Originally posted by PEACHY:
Please help me or shoot me.
BANG! icon_diablo.gif

Seriously though, lets assume that you already have your recorded macro. If you press Alt&F11 you will be taken to the Visual Basic Editor (VBE). Here you should see, in the Project window, the VBAProject associated with your workbook. Under the project will be a folder called 'Modules' - double-click on this and it will show that it contains a module called 'Module1'. Within this module you will see the macro that you recorded, make a note of the name (probably Macro1).

Now, go back to the Excel window where you have added the Command Button from the Controls Toolbox. Double-click on the CommandButton and you will be taken back to the VBE between some lines of code like this:
code:
Private Sub CommandButton1_Click()

End Sub


Between those lines write the name of your macro (Macro1). Alternatively, you can type 'Module1' immediately followed by a full stop (.) - this will then give you the option to choose from the macros contained in Module1.

Now go back to Excel and exit Desin Mode. That's it, whenever you click the button your routine will be executed.

HTH
_________________
Richie

Post Sat Feb 14, 2004 11:13 am 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.