writing vba code in sheet1 instead of module

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I opened bran new excel file then went to vba editor. I see Sheet1(sheet1) and Thisworkbook, inside Project windows. I double clicked sheet1 and started typing this code:

Code:
Sub abc()
    MsgBox "hello"
End Sub

Then I went to excel and inserted Button and assign it to that macro and I ran it and it did work fine.
Then I created a new sheet in excel by clicking on + then I insert another Button and assigned to the same Macro above. To my surprise, it did work? I thought because I wrote the macro inside Sheet1 not Module then that means it only work for Sheet1 not outside sheet1? Any help would be appreciated very much. Thank you.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
As you have discovered it works regardless of where the call comes from.
However if you used something like
Code:
Range("A1:B2").Interior.Color=56789
Then it will work on Sheet1, regardless of which sheet is active
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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