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.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Does anyone know a macro for entering a cell value (date) in cell G1 when opening the Excel file?

Thanks in advance.


in the Thisworkbook module

Code:
Private Sub Workbook_Open()
 
sheet1.range("g1").value=format(now(),"dd/mm/yyyy")
 
 
End Sub
[CODE]
 
Sheet1 can be replaced by what ever works for you
 
Upvote 0
in the Thisworkbook module

Code:
Private Sub Workbook_Open()
 
sheet1.range("g1").value=format(now(),"dd/mm/yyyy")
 
 
End Sub

Sheet1 can be replaced by what ever works for you

Why not just use

Code:
Private Sub Workbook_Open()
Range("G1") = Date
End Sub

although it seems like a pointless event when a simple worksheet function can perform the same task more effectively.
 
Upvote 0
Why not just use

Code:
Private Sub Workbook_Open()
Range("G1") = Date
End Sub

although it seems like a pointless event when a simple worksheet function can perform the same task more effectively.

What I am looking for the following:

After I open my Excel file, I want that a message box pops up, and asks me: What is your Begin Date?
And after I enter my Begin date, like 04/01/2011 it should be put in cell G1.

Anyone any idea how this could be done with a macro?
 
Upvote 0
What I am looking for the following:

After I open my Excel file, I want that a message box pops up, and asks me: What is your Begin Date?
And after I enter my Begin date, like 04/01/2011 it should be put in cell G1.

Anyone any idea how this could be done with a macro?

Code:
Private Sub Workbook_Open()
Dim vDate As Variant
vDate = InputBox("Whats my Begin Date", "Begin Date Input", Format(Now(), "dd/mm/yyyy"))

If IsDate(vDate) Then
    Sheet1.Range("g1").Value = vDate
Else
    MsgBox "No Begin date entered"
'   Sheet1.Range("g1").Value =""
End If

End Sub
[Code]
 
you might want to think about what happens if they don't enter a date
i've commented out the cell reset
 
Upvote 0
If you ask the right question then you get the right answer.

Code:
Private Sub Workbook_Open()
sheet1.range("G3") = datevalue(inputbox("What is your start date?")) 
End Sub

note this must be in the "ThisWorkbook" module.
 
Upvote 0
Great!!!! This is what I was looking for, but one question more:

Is it also possible to let the user go back and enter the correct Begin Date when entered in wrong format. Like instead of entering 04/01/2011 they just entered "TEST".

I mean is it possible to build it in in this macro:

Code:
Private Sub Workbook_Open()
Dim vDate As Variant
vDate = InputBox("Whats my Begin Date", "Begin Date Input", Format(Now(), "dd/mm/yyyy"))

If IsDate(vDate) Then
Sheet1.Range("g1").Value = vDate
Else
MsgBox "No Begin date entered"
' Sheet1.Range("g1").Value =""
End If

End Sub
[Code]
 
Upvote 0
Force a loop with an error test

Code:
Private Sub Workbook_Open()
line2:
On Error GoTo line1
Sheet1.Range("G3") = DateValue(InputBox("What is your start date?"))
On Error GoTo 0
Exit Sub
line1:
MsgBox "Not a valid date Please try again", vbOKOnly
Resume line2
End Sub
 
Upvote 0
Great!!!! This is what I was looking for, but one question more:

Is it also possible to let the user go back and enter the correct Begin Date when entered in wrong format. Like instead of entering 04/01/2011 they just entered "TEST".

I mean is it possible to build it in in this macro:

Code:
Private Sub Workbook_Open()
Dim vDate As Variant
vDate = InputBox("Whats my Begin Date", "Begin Date Input", Format(Now(), "dd/mm/yyyy"))
 
If IsDate(vDate) Then
Sheet1.Range("g1").Value = vDate
Else
MsgBox "No Begin date entered"
' Sheet1.Range("g1").Value =""
End If
 
End Sub
[Code][/QUOTE]
 
 
[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]
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,696
Members
452,938
Latest member
babeneker

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