Link to home
Start Free TrialLog in
Avatar of BigWill5112
BigWill5112

asked on

simple macro button

Why will the code below work fine when stored in a module but as soon as I put it in a button and the sub becomes private it bombs out at Range("E2").Select
Private Sub CommandButton1_Click()

'
' Test Macro
'

'
    Sheets("PL and CF details").Select
    Range("E2").Select

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Change it to Public.
Avatar of BigWill5112
BigWill5112

ASKER

Still doesn't work when changing it to public.  Still errors out at the range E2 line.
The exact same code works fine when put on a button on a userform.  So I solved my problem but would like to know why the button on a worksheet doesn't work.
>> The exact same code works fine when put on a button on a userform.  So I solved my problem but would like to know why the button on a worksheet doesn't work.

That's easy!

When the button is on the worksheet then the focus is that sheet.  The first line changes the focus to the second sheet and therefore any attempt to select the range on the first sheet fails.

It will work fine if you use:

    Sheets("PL and CF details").Select
    Sheets("PL and CF details").Range("E2").Select

Chris
So you can't have a button on a sheet that can access other sheets?  Just want to confirm Chris.  Thanks for your response!
I mean...you always have to do all that classifying stuff when you have a button that you want to access other sheets???
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't like your answer, lol.  Kills my macro recording skills.  guess I will have to suffer writing the code.   I know - you consider it easy.  all jokes aside - THX!