VBA that runs automatically when open

pretzel

New Member
Joined
Oct 2, 2014
Messages
18
Hello peeps, I am so new to VBA and it's giving me quite a challenge.

I need to create a function or perhaps a procedure that will run the check by itself when I open up my workbook. But I already have a Private Sub Workbook_Open() written in my ThisWorkbook object so I don't think adding on to the list works. I have tried Sub Auto_Open() in normal modules but it isn't working. My script as below:

Sub Auto_Open()
With Sheets("Sheet1")
.Select

Dim intYear As Integer
Dim intMonth As Integer
Dim intDay As Integer
Dim dtmNewDate As Date

intYear = DatePart("yyyy", Range("A2"))
intMonth = DatePart("m", Range("A2"))
intDay = DatePart("d", Range("A2"))

dtmNewDate = DateSerial(intYear, intMonth, intDay)

Dim nowYear As Integer
Dim nowMonth As Integer
Dim nowDay As Integer
Dim today As Date

nowYear = DatePart("yyyy", Now)
nowYear = DatePart("m", Now)
nowYear = DatePart("d", Now)

today = DateSerial(nowYear, nowMonth, nowDay)

If dtmNewDate = today Then
MsgBox "Update inventory xyz!"
Else
Calculate
End If
End With

End Sub

Basically I have a list of dates from range(A2:A100). So if today's date falls on the same day (preferably checking against only day and month), I want my msgbox to tell me which items to update. I am testing out on my first item.

Please do correct my mistakes or critique my script. It looks a little lengthy.

Thanks!
 
But do you mean to pull from cell A5 every time, or do you wish to go down the list?
And what is the point of pulling each out into variables (intYear, intMonth, and intDate), only to put them back together in dtmNewDate?

Ohhh I get what you mean. Yea that makes sense. Probably initially I was thinking that what if someone sends me a file that has the date incorrectly formatted in numbers or something. Thanks for pointing out :)
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

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