Changing the date format in InputBox to AutoFilter by

AceSpace

New Member
Joined
Jul 22, 2024
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hello! I would like to know if there's a way to accept a date in the dd.mm.yyyy format in an InputBox and then use it for the AutoFilter by date function (which to my knowledge uses the mm/dd/yyyy format). What should I do?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If you are talking about InputBox you must be developing a VBA solution but you haven't shown any of your other code, so I'm not sure where you're going with this.

Here is how you get a proper date string based on the user input you described. This is just a framework; you have to incorporate this into your code.
VBA Code:
Public Sub convertit()

   Dim DateString As String
   Dim DateD As Date
   Dim FilterDate As String
  
   DateString = InputBox(prompt:="Enter date dd.mm.yyyy", Title:="Enter Date")

   If DateString = "" Then
      MsgBox "User cancelled."
   ElseIf Not DateString Like "[0-9][0-9].[0-1][0-9].20[0-3][0-9]" And _
          Not DateString Like "[0-9][0-9].[0-1][0-9].19[0-9][0-9]" _
      Then
      MsgBox DateString & " is not a valid date in the form dd.mm.yyyy"
   Else
      On Error GoTo BadDate
      DateD = DateValue(Left(DateString, 2) & "/" & Mid(DateString, 4, 2) & "/" & Right(DateString, 4))
      On Error GoTo 0
     
      FilterDate = Format(DateD, "m/d/yyyy")
      MsgBox "Use " & FilterDate & " to build AutoFilter"
     
   End If
  
   Exit Sub
  
BadDate:
   MsgBox DateString & " is in the correct format but is not a valid date"
  
End Sub

Why don't just require the user to enter the date in mm/dd/yyyy format to begin with?
 
Upvote 0

Forum statistics

Threads
1,220,962
Messages
6,157,087
Members
451,396
Latest member
jdarner

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