how to write a macro independent of another

zacuk

Board Regular
Joined
Dec 22, 2016
Messages
60
Hi,

I have a macro which works fine. Then, I wanted to write another macro, so I clicked on Record Macro (in Code section of the Developer tab), gave it a name, stopped recording. The new macro name appears as expected in the code section of the Developer tab under Macros. HOWEVER, when I open this new darn macro to edit it, to my surprise, it also contains the previous macro in it which I want to run independently.

See, this is the 'tail end' of the first macro, which is still showing up (along with the full first macro, I have deleted most of which in the example below) in Macro 6 which I want to write and execute independent of the first macro:

.NumberFormat = "@"
.Columns.AutoFit
.Borders.Weight = 2
End With
End Sub



Sub Macro6()
'
' Macro6 Macro
'


'
End Sub

If I delete the first macro code from Macro 6, it removes the first macro from the Macros list altogether!!

Please help. Thanks
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi, you can have more than one macro in a code module. They can still be executed independently.
 
Upvote 0
Hi FormR,

How would you refer to them when 'linking' a macro to a button in Excel please?

If it is an independent macro, one can simply click on it to assign it to the button.

Thanks
 
Upvote 0
Your new Marco is named Macro6, when you assign the Macro to your button just assign that Macro6 name and only that code will run, nothing else.

The view you have is just a container, it probably is separated by a line between the two macro's, even if it isn't you see the "End Sub" command, that ends the previous macro, so only what is between the 'Sub Macro6()' and the 'End Sub' for Macro6 will ever be executed when you call Macro6.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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