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
Change it to Public.
ASKER
Still doesn't work when changing it to public. Still errors out at the range E2 line.
ASKER
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").Sele ct
Chris
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").Sele
Chris
ASKER
So you can't have a button on a sheet that can access other sheets? Just want to confirm Chris. Thanks for your response!
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!