Run Macro

R1chard

Active Member
Joined
Jan 14, 2004
Messages
407
I have the following Macro:

Private Sub Worksheet_Activate()
'
' Autosort Macro
' Macro recorded 07/02/04 by PCMS GROUP
'autosort
Range("B12:I191").Select
Range("I12").Activate
Selection.Sort Key1:=Range("I12"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
End Sub

How can i get this macro to run automaticaly when i open the workbook. There is only one sheet to the spreadsheet, and I do not want to use a button. I would like the macro to run when the spreadsheet is opened.

Thanks :wink:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this

Private Sub Workbook_Open()

' Autosort Macro
' Macro recorded 07/02/04 by PCMS GROUP
'autosort
sheets("enter sheet name here").select
Range("B12:I191").Select
Range("I12").Activate
Selection.Sort Key1:=Range("I12"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
End Sub
 
Upvote 0
To have the procedure run when you open the workbook, place the code in the workbook module as a Workbook_Open event, or in a standard module as an Auto_Open procedure.

Examples (pick one, do NOT do both):

Place this in your workbook module and see if it accomplishes what you are after. To easily access your workbook module, find the little Excel workbook icon near the upper left corner of your workbook window, usually just to the left of the File menu option. Right click on that icon, left click on View Code, and paste the following procedure into the large white area that is the workbook module. Press Alt+Q to return to the worksheet.

Sub Workbook_Open()
Range("B12:I191").Sort Key1:=Range("I12"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
End Sub

or

In a standard module:

Sub Auto_Open()
Range("B12:I191").Sort Key1:=Range("I12"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1").Select
End Sub

It is curious that if you only have one sheet, why you would need to have an open event take place as long as you have a sheet activation event in place for this procedure. The sheet can only be the one activated upon workbook open, so your procedure should fire if you have that code where it belongs in the sheet module.

Anyway, try the above suggestions, and then delete the existing Sheet Activate code because it will be unnecessary, and post back how it worked out for you.
 
Upvote 0

Forum statistics

Threads
1,203,608
Messages
6,056,286
Members
444,855
Latest member
archadiel

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