Putting a date into a cell from an input box

Red over White

Board Regular
Joined
Jul 16, 2011
Messages
127
Office Version
  1. 365
Platform
  1. MacOS
The solution is probably very simple, but is beyond me. I have been trying to resolve the above problem, which is part of a much bigger macro in which I have been able to put various strings and numbers in successfully by using input boxes. The specific code I am struggling with:

Sub InputDate()

Dim Caption2G As String
Dim Prompt2G As String
Dim EDate As Date
Caption2G = "Maturity Date"
Prompt2G = "Enter Date"
EDate = Val(InputBox(Prompt2G, Caption2G, Format(EDate, "dd/mm/yyyy")))

ActiveSheet.Cells(1, 1) = EDate

End Sub

When the Input Box the date "31/12/1899" appears which I'm not too bothered about (unless I am missing something fundamental). It will accept a new date but when it is transferred to cell A1 only the "dd" element of the date appears correctly, the rest of the date appears as "/01/00". A1 is formatted "dd/mm/yy".

How do I get the whole of the date, for example "02/08/2023" (the date will always be later than today), transferred to A1 so that it shows in the correct "dd/mm/yy" format?

Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I don't think you need to use Val, or Format here.
VBA Code:
Sub InputDate()
Dim EDate As Date
EDate = InputBox("Enter Date", "Maturity Date", Format(Date, "dd/mm/yyyy"))
ActiveSheet.Cells(1, 1) = EDate
End Sub

Edited to include formatting for dd/mm/yyyy default entry.
 
Upvote 0
Heck, the entire macro could just be one line (excluding the Sub/EndSub)
VBA Code:
Sub InputDate()
ActiveSheet.Cells(1, 1) = InputBox("Enter Date", "Maturity Date", Format(Date, "dd/mm/yyyy"))
End Sub
 
Upvote 0
Solution

pjoaquin

Thank you - I knew it would be a simple answer! Both work brilliantly. Your response also means I can shorten the larger macro this section was a part of by getting rid of unnecessary dim lines.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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