Strange Macro Behavior

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am getting a strange thing.

Using Excel's "Record Macro" wizard, I generated the following code:

Sub Macro10()
'
' Macro10 Macro
Range("B7").Select
Selection.Copy
Sheets("Quotes Database").Select
Range("J5").Select
ActiveSheet.Paste
End Sub

If I then run Macro10 it works fine.

However, if I go to Developer on the Ribbon and then insert a rectangular command button and assign the same code to it, then when I click on the button, an error 1004 is generated.

If I insert a regular shape and assign the code, the macro works OK.

Why does it not work for the inserted command button?

Does anyone know what is going on? Can you replicate this? I'm using 2007.

Thanks,

MikeG
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Thanks Jen.

But the macro works fine when I run it as Macro10, or from a regular shape. The same code does not work on an inserted command button - that is my issue.

MikeG
 
Upvote 0
code behaves differently depending on how it's called...

To be sure to avoid problems like this it is usually best to explicitly name the sheets involved in your code.
And to avoid Selecting, it is almost never necessary to select a sheet/range in order to manipulate it.

Try it like this

Sub Macro10()
'
' Macro10 Macro
Sheets("SourceSheet").Range("B7").Copy Destination:=Sheets("Quotes Database").Range("J5")
End Sub

Adjust the SourceSheet to reflect the sheet that you are copying from.


Hope that helps.
 
Upvote 0
insert a rectangular command button
an ActiveX button I take it
and assign the same code to it
meaning you copied the code into the CommandButton_Click event handler

in which case, the event handler code is in a sheet code-module and unqualified ranges refer to that sheet, once you've activated another sheet, you can't then select a cell on the first sheet without an error being thrown.

Solution, either:
Code:
Range("B7").Select
Selection.Copy
With Sheets("Quotes Database")
    .Select
    .Range("J5").Select
    .Paste
End With
or just :
Code:
Range("B7").Copy Sheets("Quotes Database").Range("J5")
 
Upvote 0
Thanks guys - it was driving me nuts!

MikeG
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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
Back
Top