Date Formatting in VBA code not working properly

ArleneJo

New Member
Joined
Oct 8, 2012
Messages
8
Hi,

I've written my first code that will require user's input of a date. I want to format the date to be shown in a cell as "dd/mm/yyyy". Everything works but the output is showing as mm/dd/yyyy. Can anybody help me please?

Code:
 Sub inputSettlementDate()

Dim varInputDate As Variant
Dim lngERow As Long




varInputDate = InputBox("Please enter the Settlement Date", "Settlement Date", "dd/mm/yyyy")




If IsDate(varInputDate) Then
  varInputDate = Format(varInputDate, "dd/mm/yyyy")
   lngERow = Range("B" & Rows.Count).End(xlUp).Row + 1
   Range("B" & lngERow).Value = varInputDate


   Else


   MsgBox "Please enter a valid date format dd/mm/yyyy"
   varInputDate = InputBox("Please enter the Settlement Date using this format dd/mm/yyyy.", "Settlement Date")
End If




End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
It seems the cell you are putting the date into......Range("B" & lngERow).Value is formatted as mm/dd/yyyy
It's not the code that's causing the difference, so add this line...in red

Rich (BB code):
Sub inputSettlementDate()
Dim varInputDate As Variant
Dim lngERow As Long
varInputDate = InputBox("Please enter the Settlement Date", "Settlement Date", "dd/mm/yyyy")
If IsDate(varInputDate) Then
varInputDate = Format(varInputDate, "dd/mm/yyyy")
lngERow = Range("B" & Rows.Count).End(xlUp).Row + 1
Range("B" & lngERow).Value = varInputDate
Range("B" & lngERow).NumberFormat = "dd/mm/yyyy" 
Else
MsgBox "Please enter a valid date format dd/mm/yyyy"
varInputDate = InputBox("Please enter the Settlement Date using this format dd/mm/yyyy.", "Settlement Date")
End If
End Sub
 
Upvote 0
Try this.
Code:
If IsDate(varInputDate) Then

lngERow = Range("B" & Rows.Count).End(xlUp).Row + 1
With Range("B" & lngERow)
    .Value = DateValue(varInputDate)
    .NumberFormat ="dd/mm/yyyy"
End With


Else
 
Upvote 0
Hi,
see if this update to your code helps:

Code:
Sub inputSettlementDate()


    Dim varInputDate As Variant
    Dim lngERow As Long
    
    Do
    varInputDate = InputBox("Please enter the Settlement Date", "Settlement Date", "dd/mm/yyyy")
    'cancel pressed
    If StrPtr(varInputDate) = 0 Then Exit Sub
    
    If Not IsDate(varInputDate) Then MsgBox "Please enter a valid date format dd/mm/yyyy", 16, "Invalid Date"
    
    Loop Until IsDate(varInputDate)
    
    lngERow = Range("B" & Rows.Count).End(xlUp).Row + 1
    
    With Range("B" & lngERow)
        .Value = DateValue(varInputDate)
        .NumberFormat = "dd/mm/yyyy"
    End With


End Sub

Dave
 
Upvote 0
thanks guys for looking into my query. I thought IsDate is enough to qualify both the response from the input box and the property of its cell destination.

Hi,
see if this update to your code helps:

Code:
Sub inputSettlementDate()


    Dim varInputDate As Variant
    Dim lngERow As Long
    
    Do
    varInputDate = InputBox("Please enter the Settlement Date", "Settlement Date", "dd/mm/yyyy")
    'cancel pressed
    If StrPtr(varInputDate) = 0 Then Exit Sub
    
    If Not IsDate(varInputDate) Then MsgBox "Please enter a valid date format dd/mm/yyyy", 16, "Invalid Date"
    
    Loop Until IsDate(varInputDate)
    
    lngERow = Range("B" & Rows.Count).End(xlUp).Row + 1
    
    With Range("B" & lngERow)
        .Value = DateValue(varInputDate)
        .NumberFormat = "dd/mm/yyyy"
    End With


End Sub

Dave
 
Upvote 0
thanks guys for looking into my query. I thought IsDate is enough to qualify both the response from the input box and the property of its cell destination.

Hi,

Your Variant variable varInputDate is checked using IsDate to ensure user has entered something that is recognisable as a valid date.
IsDate Function Returns a Boolean value - True if the value is recognizable as a valid date or time; otherwise, it returns False.

What comes out of the InputBox Function is a string containing the contents of users input.
To change a string date from your inputbox to a date data type you would use DateValue Function.

DateValue recognizes the order for month, day, and year according to the Short Date format you specified for your system & hopefully updated solution, has now resolved your problem?

Hope Helpful

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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