Fill in a value for cell with macro, when opening Excel file

benjamint

Board Regular
Joined
Jun 24, 2009
Messages
114
Does anyone know a macro for entering a cell value (date) in cell G1 when opening the Excel file?

Thanks in advance.
 
Code:
Private Sub Workbook_Open()
Dim vDate As Variant
While Not IsDate(vDate)
vDate = InputBox("Whats my Begin Date", "Begin Date Input", Format(Now(), "dd/mm/yyyy"))
If vDate = "" Then '' they presed cancel
Exit Sub 'exit doing nothing
End If
 
If IsDate(vDate) Then
Sheet1.Range("g1").Value = vDate '' a good date has been entered
Exit Sub
Else
MsgBox "No Valid Begin Date entered"
 
End If
Wend
End Sub
[code][/QUOTE]
 
Wow, unbelievable, thank you so much!!! It helps me so much. I really appreciate your help guys!!!
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
Private Sub Workbook_Open()
Dim vDate As Variant
While Not IsDate(vDate)
vDate = InputBox("Whats my Begin Date", "Begin Date Input", Format(Now(), "dd/mm/yyyy"))
If vDate = "" Then '' they presed cancel
Exit Sub 'exit doing nothing
End If
 
If IsDate(vDate) Then
Sheet1.Range("g1").Value = vDate '' a good date has been entered
Exit Sub
Else
MsgBox "No Valid Begin Date entered"
 
End If
Wend
End Sub

Use [/code] to close the tags, you're trying to close with the opening tag, that's why it's not formatting correctly.
 
Upvote 0
One last question, is there also a possibility to build in an End date? The end date should be put on cell J1. Should this be a separate macro, or is it possible to build 1 macro for both dates, such that it first asks for Begin date and after that asking for the End date?

Once again thanks in advance.
 
Upvote 0
One last question, is there also a possibility to build in an End date? The end date should be put on cell J1. Should this be a separate macro, or is it possible to build 1 macro for both dates, such that it first asks for Begin date and after that asking for the End date?

Once again thanks in advance.

2 conditions
Suprisingly Now You have to specify what will happen

Start date cancelled, should workvbook ask about an end date?
Is valid start date plus valid end date only

or can it be start date :- a date is entered, end date is cancelled



HOW DO YOU SEE IT WORKING KNOWING HOW IMPERFECT PEOPLE ARE?
 
Upvote 0
2 conditions
Suprisingly Now You have to specify what will happen

Start date cancelled, should workvbook ask about an end date?
Is valid start date plus valid end date only

or can it be start date :- a date is entered, end date is cancelled



HOW DO YOU SEE IT WORKING KNOWING HOW IMPERFECT PEOPLE ARE?

Dear Charles, actually I want both start and end date to be valid, and only if they both are valid, then enter these values.
Is this possible?
 
Upvote 0
Maybe

Code:
Private Sub Workbook_Open()
    Dim dt1 As Long, dt2 As Long, dtcheck As Boolean
line2:
    On Error GoTo line1
        If Not dtcheck Then dt1 = DateValue(InputBox("What is your start date?"))
            dtcheck = 1
            dt2 = DateValue(InputBox("What is your finish date?"))
    If dt2 < dt1 Then
        MsgBox "Must be after Start date, please try again", vbOKOnly
        GoTo line2
    End If
On Error GoTo 0
    Sheet1.Range("G1") = dt1
    Sheet1.Range("J1") = dt2
Exit Sub
line1:
    MsgBox "Not a valid date Please try again", vbOKOnly
Resume line2
End Sub

Conditions

Valid date must be entered as start date.
Valid date must be entered as end date.
No blanks allowed, no cancel allowed, inputbox will stay until a valid date is entered.
End date must not be earlier than start date.

As a getout clause if needed, changing the last 3 lines to

Code:
line1:
    If MsgBox("Not a valid date Please try again", vbAbortRetryIgnore) = vbAbort Then ThisWorkbook.Close
Resume line2

would cause the workbook to close unedited if the user chooses to abort without entering a valid date.
 
Last edited:
Upvote 0
can the dates be the same? can start date be after end date?? all of things a user does

Charles, thanks, this was exactly what I was looking for. The date conditions are also OK, no need to change anything, if the user still succeeds to get the wrong data, then we should doubt the brain of the user!!!!:rofl::):laugh:
 
Upvote 0
Charles, thanks, this was exactly what I was looking for. The date conditions are also OK, no need to change anything, if the user still succeeds to get the wrong data, then we should doubt the brain of the user!!!!:rofl::):laugh:

If the end date is before the start date then maybe we should question if the user has a brain to doubt :)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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