Macro to attach a macro to a workbook

paulnray

New Member
Joined
May 24, 2005
Messages
29
Hi All,

I have noticed that allthough I have been autofitting my data on each sheet in my workbooks when people have opened them the autofit has gone so I have added some code so when they open the workbook it runs through and autofits the sheets this is working fine.

My question is: As I have about 28 workbooks several times a month that would need this code to be added to is it possible to create a macro that copies the following code to each workbook in a specific folder, lets just say "my documents" for now or will i need to add this code manually?

The code is as follows: -

Private Sub Workbook_Open()
Dim wrksht As Worksheet
For Each wrksht In Worksheets

Application.ScreenUpdating = False
wrksht.Select
Cells.EntireColumn.AutoFit

Next wrksht
Sheets(1).Select
Application.ScreenUpdating = True
End Sub

Any help you can give me would be appreciated.

Regards

Paul
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
HALLO

You could put the routine in a file. Xla version Excel2003 so run every time you open the file in question.

Do not copy worksheets in vba code
 
Upvote 0
Ok thanks for that something else to play with. Just been reading about XLA and you have to turn the property on for it to run the file would I have to set that on each workbook or can this be done via a macro?

Regards

Paul
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,254
Members
452,900
Latest member
LisaGo

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