Global VBA date format and decimal separator

此生再无相见时 提交于 2019-12-12 03:37:54

问题


Is there a way to change VBA settings globally on PC to accept dates and number on a specified format? (on my case dd/mm/yyyy and comma)

Changing Excel settings doesn't solve it for me.

As an small time VBA developer, I'm mostly creating userforms for data input and validation. Alongside with some basic access privileges, It keeps users (mostly an client's hired arms) from nosing on the database and corrupting it.

But, on form's submitting, the textbox values are saved temporally on spreadsheet's cells. Somehow on this step dates get scrambled and in some cases an 3 decimal places numeric gets multiplied by a thousand (e.g. 1/2/2000 turn to 2/1/2000 and 1,234 turn 1234). It defeats the whole purpose of those applications - data gets corrupted.

I've been able to workaround these using the Format(expression, format) function, but that must be applied every time an date or some precision number is added, or even used on some auxiliary task.

This is an recurrent problem for me because, as an Brazilian, dates are formatted as dd/mm/yyyy and decimal separator is ","(comma) on practically 100% of my local users.

Anybody had similar problems?

TIA


回答1:


Excel doesn't have a default date format. Excel uses the Window System Date format settings. You can change you system setting by go to Control Panel -> Change date, time and number formats.

Change Date Format in Windows 7, 8.1 and Windows 10 to dd-mm-yyyy


After adjusting the Windows System Settings to dd-mm-yyyy, CDate will expect strings to be in the dd-mm-yyyy.

Range("A1").Value = CDate( "11/01/2016" )

Result: Monday, January 11, 2016




回答2:


Summary of comments for those to lazy to read.

Alright, as Thomas Inzina pointed, the strait answer to my question is NO, you can't because there isn't such thing in VBA as this Global setting.

As Rory pointed out, the CDate function should solve (indeed it does) this issue, at least as to the date. Again, Thomas answer didn't include it but it points to the windows conf that would be used by the CDate function.

Datetimepicker, suggested by cyboashu, would solve this issue too, but it requires some tweaking on the user's PC to be available. Too much work for me. Although, this approach has the "pretty" advantage, adds value to your project.

Still looking for the comma/dot bad conversion problem. I'll keep editing this answer while none better exists.




回答3:


Here I write a function called gdate that accepts a string with a date in a specific format. Then I parse the string, then call cdate based on the users date settings.

All I have to do is find/replace cdate with gdate throughout my code. Now I have a way to handle all date formats by always expecting an exact one gdate("mm/dd/yyyy"). Adjust the parsing if you want to expect different format. Building off built-in objects and functions is how we make things work.

Function gdate(ByVal dstring As String)
    '  0 = month-day-year;   1 = day-month-year;   2 = year-month-day
    d = Mid(dstring, InStr(1, dstring, "/") + 1, Len(dstring) - 5 - InStr(1, dstring, "/"))
    m = Left(dstring, InStr(1, dstring, "/") - 1)
    y = Right(dstring, 4)
    dtype = Application.International(xlDateOrder)

    Select Case dtype
        Case 0: gdate = CDate(m & "/" & d & "/" & y)
        Case 1: gdate = CDate(d & "/" & m & "/" & y)
        Case 2: gdate = CDate(y & "/" & m & "/" & d)
    End Select

End Function


来源:https://stackoverflow.com/questions/38269669/global-vba-date-format-and-decimal-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!