Link to home
Start Free TrialLog in
Avatar of CarenC
CarenC

asked on

Excel VBA - Date in different language settings

Using Excel 2010, the user selects a date in a cell.  Code converts the date to a string to use in the query.  This is the string format: "CONVERT(DATETIME, '2013-06-30 00:00:00', 102) ".   If the user has a language setting other than English the query doesn't work because the date doesn't convert to the string in the format shown.  Having the user change language settings is not an option so how can I get the date into a format that will work in the query?  

Code:

Public Sub GetDates()
    Worksheets("Select Date Range").Select
    Range("C3").Select
    dStartDate = Selection.Value
    Range("C4").Select
    dEndDate = Selection.Value
   
    ' convert date to format needed for queries
    ' CONVERT(DATETIME, '2013-06-30 11:59:59', 102)
    cStartDate = "CONVERT(DATETIME, '" & dStartDate & " 00:00:00', 102)"
    cEndDate = "CONVERT(DATETIME, '" & dEndDate & " 00:00:00', 102)"
End Sub
SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial