how to run a macro automatically in a particular sheet

tabspace

New Member
Joined
Oct 23, 2017
Messages
16
I have the following macro:
Code:
Sub Do_it()

If Now > 43466 Then Range("C:D,F:F").EntireColumn.Hidden = False ' 43466 is Jan 1 2019
If Now > 43497 Then Range("K:K,P:P").EntireColumn.Hidden = True ' 43497 is Feb 1 2019
If Now > 43525 And Now < 43534 Then Range("W:Z").EntireColumn.Hidden = False  ' march 1~10 2019

End Sub

I want it to run automatically when the file is opened, but only in 2 particular sheets, lets say "SHEETA" and "SHEETB"

Can anybody help me with this?

Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
(Untested) Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Save the workbook as a macro-enabled file. Close it and then re-open it.
Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets(Array("SHEETA", "SHEETB"))
        If Now > 43466 Then ws.Range("C:D,F:F").EntireColumn.Hidden = False ' 43466 is Jan 1 2019
        If Now > 43497 Then ws.Range("K:K,P:P").EntireColumn.Hidden = True ' 43497 is Feb 1 2019
        If Now > 43525 And Now < 43534 Then ws.Range("W:Z").EntireColumn.Hidden = False  ' march 1~10 2019
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,959
Messages
6,122,476
Members
449,087
Latest member
RExcelSearch

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