VBA: Macro won't run

FunsizedNerd

New Member
Joined
Mar 20, 2019
Messages
17
I have opened a new workbook with data. I have created a command button and copied over some code to allow it to float

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error GoTo 0
With Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn)
CommandButton1.Top = .Top + 100
CommandButton1.Left = .Left + 300
End With
End Sub

However, when I try to run this macro (both green play button and F5 shortcut) it doesn't run and comes up with the Macros pop-up form (to select a macro to run)

If I create a different macro, the new one comes up, but this one doesn't
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
The code is run by an event Selection change. Select a different cell in the workbook and see if it works.
 
Upvote 0
Why did you use this name?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
That is a reserved name for a specifc Event Procedure VBA code. Event procedure run automatically, upon some event happening. You do NOT physically call/run the macro.
The Worksheet_SelectionChange event runs automatically whenever a cell is selected.

If you want something that you can attach to a command button and/or run manually, you do NOT want it to be an Event Procedure. Choose some other name.
Also, the word "Private" will hide that macro so it will not be seen from the list when you elect to "Run Macros". You want to remove the word "Private" from any custom code that you want to be seen from that menu.
 
Upvote 0
I didn't choose this name - it was code that was written by someone else, I used it as it was a solution to the problem I have. I'll try changing it.
 
Upvote 0
I didn't choose this name - it was code that was written by someone else, I used it as it was a solution to the problem I have. I'll try changing it.
You may be able to still use it, if you realize how it works (and that serves your needs). Just understand that you cannot attach it to any button or run it on command manually.
It runs automatically whenever you select a new cell on that particular sheet.
 
Last edited:
Upvote 0
You may be able to still use it, if you realize how it works (and that serves your needs). Just understand that you cannot attach it to any button or run it on command manually.
It runs automatically whenever you select a new cell on that particular sheet.

Well, I don't know how it works, otherwise I probably wouldn't be asking this question. It's a bit of code that kept coming up when I searched for a way to make a command button float, so I assumed it would work.
 
Upvote 0
Was this code pasted into the sheet module or a standard module ?
 
Upvote 0
Well, I don't know how it works, otherwise I probably wouldn't be asking this question.
I realize that, which is why I am explaining it to you in the following two lines:
Just understand that you cannot attach it to any button or run it on command manually.
It runs automatically whenever you select a new cell on that particular sheet.
If you don't understand how the existing code runs or what it is doing, then you probably do not want to mess with it.

Let's go back to square one.
Can you tell us exactly what it is that you are trying to do, what you want to happen?
It may be that it should be a totally separate VBA procedure (probably, if you want to link it to a command button).
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,263
Members
449,307
Latest member
Andile

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