I'm having some difficulties with my datepicker date. StartDate and EndDate are values from DatePicker and Debug.Print will give me "mm/dd/yyyy". I am trying to change the date format to "dd/mmm/yyyy" so that I will be able to compare it with my data from MS access. But it's not working.
Sub cmdSubmit_Click()
Dim StartDate As Date, EndDate As Date
StartDate = DTPickStart.Value ' 6/11/2018
EndDate = DTPickEnd.Value ' 6/24/2018
StartDate = Format(StartDate, "dd/mmm/yyyy")
EndDate = Format(EndDate, "dd/mmm/yyyy")
Debug.Print StartDate ' gave me 6/11/2018 instead of 11/Jun/2018
Debug.Print EndDate
End Sub
Any advice on what I am doing wrong here?
Thanks in advance!
Format probably returns the date in the format you want, but it is then transformed into a date-value which is then printed in the default format for your system. To get them stored in the format you want, you could try something like:
Option Explicit
Sub cmdSubmit_Click()
Dim StartDate As Date, EndDate As Date
Dim s1 As String, s2 As String
StartDate = #6/11/2018#
EndDate = #6/24/2018#
'StartDate = Format(StartDate, "dd/mmm/yyyy")
'EndDate = Format(EndDate, "dd/mmm/yyyy")
s1 = Format(StartDate, "dd/mmm/yyyy")
s2 = Format(EndDate, "dd/mmm/yyyy")
Debug.Print StartDate & " - " & s1 ' gave me 6/11/2018 instead of 11/Jun/2018
Debug.Print EndDate & " - " & s2
End Sub
Alternately if you want them in a cell as dates, you could set the NumberFormat
of the range you write them to, to be in your wanted format:
Sheet1.Range("A1") = StartDate
Sheet1.Range("A2") = EndDate
Sheet1.Range("A1:A2").NumberFormat = "dd.mmm.yyyy"
If you need to format the date in VBA you have 2 options:
.Text
: Option Explicit
Sub TestMe()
Dim StartDate As Date
StartDate = #6/11/2018#
Cells(1, 1) = StartDate
Cells(1, 1).NumberFormat = "DD/MMM/YYYY"
Debug.Print Cells(1, 1).Text '11.Jun.2018
End Sub
As per the MSDN Application.International(Index) method returns information about the current country/region and international settings.
When you apply index as xlDateOrder
0 = month-day-year
1 = day-month-year
2 = year-month-day
Hence, we will be using this property to override the system behavior. This is especially useful when the Excel-VBA project is used across geographies with various individual regional settings.
Below, I am re-writing your code with a minor tweak. Format function returns the string but you are assigning it to a date which is causing this issue.
Sub cmdSubmit_Click()
Dim StartDate As Date, EndDate As Date
Dim strStartDate as String, strEndDate as String
StartDate = DTPickStart.Value ' 6/11/2018
EndDate = DTPickEnd.Value ' 6/24/2018
strStartDate = Format(StartDate, "dd/mmm/yyyy")
strEndDate = Format(EndDate, "dd/mmm/yyyy")
Debug.Print StartDate ' It will give 11/Jun/2018
Debug.Print EndDate
End Sub
Thanks everyone. I managed to figure it out, but I'm giving my vote to jainashish as it's closer to how I could use the result to query my access db.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.