PC Review


Reply
Thread Tools Rate Thread

Command button to run a macro

 
 
sparkes84
Guest
Posts: n/a
 
      19th Mar 2009
Hi, really stuck. Thought I had written the code correctly but when I click
on my command button to run my macro, it comes up with 'Object required'.
When I click on Tools and then Macros, my macro on there is called
Sheet1.RemoveThem

My code looks like this - where have I gone wrong?
Private Sub CommandButton1_Click()
On Error GoTo Err_Command1_Click

Dim stMacroName As String

stMacroName = "Sheet1.RemoveThem"
DoCmd.RunMacro stMacroName

Exit_Command1_Click:
Exit Sub

Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click

End Sub

Thanks for any help
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      19th Mar 2009
Try changing

DoCmd.RunMacro stMacroName

to

Application.Run stMacroName

HTH,
Bernie
MS Excel MVP


"sparkes84" <(E-Mail Removed)> wrote in message
news:2611D0CD-D6A0-4DEC-903C-(E-Mail Removed)...
> Hi, really stuck. Thought I had written the code correctly but when I click
> on my command button to run my macro, it comes up with 'Object required'.
> When I click on Tools and then Macros, my macro on there is called
> Sheet1.RemoveThem
>
> My code looks like this - where have I gone wrong?
> Private Sub CommandButton1_Click()
> On Error GoTo Err_Command1_Click
>
> Dim stMacroName As String
>
> stMacroName = "Sheet1.RemoveThem"
> DoCmd.RunMacro stMacroName
>
> Exit_Command1_Click:
> Exit Sub
>
> Err_Command1_Click:
> MsgBox Err.Description
> Resume Exit_Command1_Click
>
> End Sub
>
> Thanks for any help



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Mar 2009
Is this code in the same worksheet module that owns the commandbutton (Sheet1)?

If yes, you could just call the macro directly:

Option Explicit
Private Sub CommandButton1_Click()
Call RemoveThem
End Sub
Sub RemoveThem()
MsgBox "hi"
End Sub

And if the removeme procedure is in a different worksheet module than the
commandbutton_click:


Behind the sheet with the commandbutton:
Option Explicit
Private Sub CommandButton1_Click()
Call Sheet1.RemoveThem
End Sub

Behind Sheet1:
Option Explicit
Sub RemoveThem()
MsgBox "hi"
End Sub

Application.run doesn't make a lot of sense here -- unless you're building the
name of the procedure or the procedure is hidden/private.




sparkes84 wrote:
>
> Hi, really stuck. Thought I had written the code correctly but when I click
> on my command button to run my macro, it comes up with 'Object required'.
> When I click on Tools and then Macros, my macro on there is called
> Sheet1.RemoveThem
>
> My code looks like this - where have I gone wrong?
> Private Sub CommandButton1_Click()
> On Error GoTo Err_Command1_Click
>
> Dim stMacroName As String
>
> stMacroName = "Sheet1.RemoveThem"
> DoCmd.RunMacro stMacroName
>
> Exit_Command1_Click:
> Exit Sub
>
> Err_Command1_Click:
> MsgBox Err.Description
> Resume Exit_Command1_Click
>
> End Sub
>
> Thanks for any help


--

Dave Peterson
 
Reply With Quote
 
ryguy7272
Guest
Posts: n/a
 
      19th Mar 2009
Couple good tutorials here:
http://www.anthony-vba.kefra.com/vba/vbabasic1.htm
http://www.mrexcel.com/tip068.shtml


Good luck,
Ryan---


"Dave Peterson" wrote:

> Is this code in the same worksheet module that owns the commandbutton (Sheet1)?
>
> If yes, you could just call the macro directly:
>
> Option Explicit
> Private Sub CommandButton1_Click()
> Call RemoveThem
> End Sub
> Sub RemoveThem()
> MsgBox "hi"
> End Sub
>
> And if the removeme procedure is in a different worksheet module than the
> commandbutton_click:
>
>
> Behind the sheet with the commandbutton:
> Option Explicit
> Private Sub CommandButton1_Click()
> Call Sheet1.RemoveThem
> End Sub
>
> Behind Sheet1:
> Option Explicit
> Sub RemoveThem()
> MsgBox "hi"
> End Sub
>
> Application.run doesn't make a lot of sense here -- unless you're building the
> name of the procedure or the procedure is hidden/private.
>
>
>
>
> sparkes84 wrote:
> >
> > Hi, really stuck. Thought I had written the code correctly but when I click
> > on my command button to run my macro, it comes up with 'Object required'.
> > When I click on Tools and then Macros, my macro on there is called
> > Sheet1.RemoveThem
> >
> > My code looks like this - where have I gone wrong?
> > Private Sub CommandButton1_Click()
> > On Error GoTo Err_Command1_Click
> >
> > Dim stMacroName As String
> >
> > stMacroName = "Sheet1.RemoveThem"
> > DoCmd.RunMacro stMacroName
> >
> > Exit_Command1_Click:
> > Exit Sub
> >
> > Err_Command1_Click:
> > MsgBox Err.Description
> > Resume Exit_Command1_Click
> >
> > End Sub
> >
> > Thanks for any help

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Macro To Change Which Macro Assigned To Command Button CVinje Microsoft Excel Misc 0 25th May 2010 09:55 PM
Command button macro Mark N Microsoft Excel Misc 6 28th Aug 2009 10:31 PM
macro to date stamp to the right of the macro command button Roha Microsoft Excel Programming 4 31st Jul 2009 01:26 PM
Command Button Macro Patty Microsoft Excel Programming 1 18th May 2006 05:00 PM
Macro for command button wcmar10 Microsoft Excel Programming 0 30th Jul 2003 09:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:48 PM.