[Solved]Shortcut macro vs Button-Macro

Jouve

New Member
Joined
Aug 10, 2006
Messages
19
Hello,

i have a macro that does something like this:

Code:
Sheets("A").Select
Range("A1").Select
ActiveCell.FormulaR1C1 = "this is Sheet A"

Sheets("B").Select
Range("A1").Select
ActiveCell.FormulaR1C1 = "this is Sheet B"

Sheets("A").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "I'm back to Sheet A"

Now, i have 2 macros that implement the same code described above.
one is a Command1_click in Sheet A

the other is shortcut macro : Ctrl + r in "Modules"


If i'm pressing Ctrl + r, it works perfectly.

if i click the button in sheet A,
something like this comes out :

Laufzeitfehler "1004":

Die Methode 'Range' für das Objekt '_Worksheet' ist fehlgeschlagen





Translated to : Error 1004

The method 'Range' for the object '_Worksheet' failed.

and sometimes : The 'Select' method from the Range Object can't be executed

what's wrong here?
is there anyway to make the Button work ?

I'd appreciate any help, thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Jouve

New Member
Joined
Aug 10, 2006
Messages
19
i solved it already.
thank's for trying to help macropheliac.

it's VB button.

i just edit the Command1_click macro to execute the shortcut macro and it works :)
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
Hello Jouve,
Yes, calling the code in the standard module from the command button is a perfectly acceptable way to get around the situation you were in but you can do it with the command button as well. The reason it wasn't working the first time is because a CommandButton's code is sheet specific (as you found by it residing in the sheet module - and wouldn't select a range in another sheet), whereas the code in a standard module can be called from anywhere. The important thing to note though is that these sheets/cells don't have to be selected to work with them, merely referenced in the code so you can indeed use your commandbutton to perform actions in another sheet. (No matter where your code is stored/called from it'll run cleaner & faster if you just reference your objects instead of actually selecting them)
For an example of what I mean, try this with your commandbutton.
Code:
Private Sub Command1_Click()
Sheets("A").Range("A1").Value = "this is Sheet A"

Sheets("B").Range("A1").Value = "this is Sheet B"

Sheets("A").Range("A2").Value = "I'm back to Sheet A"
End Sub

Does that help clear things up a little?
 

Forum statistics

Threads
1,141,626
Messages
5,707,489
Members
421,510
Latest member
haroonstr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top