Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 为什么Cdate()只与英语月份一起工作?_Excel_Vba - Fatal编程技术网

Excel 为什么Cdate()只与英语月份一起工作?

Excel 为什么Cdate()只与英语月份一起工作?,excel,vba,Excel,Vba,我有一个下拉框,项目取决于区域设置: Private Sub UserForm_Initialize() Select Case Application.International(XlApplicationInternational.xlCountryCode) Case 1: 'English With ComboBox1 .AddItem "January" ...etc End With Case 36: 'Hungarian

我有一个下拉框,项目取决于区域设置:

Private Sub UserForm_Initialize()
Select Case Application.International(XlApplicationInternational.xlCountryCode)
   Case 1: 'English
   With ComboBox1
        .AddItem "January"
        ...etc
   End With
   Case 36: 'Hungarian
   With ComboBox1
        .AddItem "Január"
        ...etc
   End with
   Case 49: 'German
        With ComboBox1
           .AddItem "Januar"
           ...etc
   End with
End Select
End Sub
稍后,我将在此代码中使用选定的值:

Year_1 = 2017 'integer
Day_1 = 1 'integer
Date_from_userform = CDate(Year_1 & "-" & UserForm1.ComboBox1.Value & "-" & Day_1) 'date
在德国的环境中,它工作得非常完美,但我在匈牙利的环境中进行了测试,每次都会出现类型不匹配

Cdate不接受2017年Január-1。(他是匈牙利人)为什么

如果月份取决于区域设置,它应该可以工作。。。
(或者我应该将下拉框中的值转换为数字吗?

我将使用以下方法为您编写月份“名称”,而不是自己编写所有月份(可能存在拼写错误的风险):

之后,您可以使用相同的代码轻松浏览所有可能的月份名称并将其转换回日期:

strDate = "2017-Januar-05"

For i = 1 To 12
    strDate = Replace(strDate, Application.WorksheetFunction.Text(DateSerial(2017, i, 1), "[$-de-DE]MMMM"), i)
Next i

Debug.Print IsDate(strDate)
Debug.Print CDate(strDate)

我将使用Format函数、DateSerial函数和ComboBox1.ListIndex属性

Private Sub CommandButton1_Click()
 Year_1 = 2017 'integer
 Day_1 = 1 'integer
 Date_from_userform = DateSerial(Year_1, ComboBox1.ListIndex + 1, Day_1)
End Sub

Private Sub UserForm_Initialize()
 For i = 1 To 12
  ComboBox1.AddItem Format(DateSerial(Year(Date), i, 1), "mmmm")
 Next
End Sub

CDate
尊重Windows区域设置,而不是Excel语言。在任何情况下,您都不应解析本地化的月份名称。在下拉列表中创建一个,并为标题(本地化的月份名称)和值(月份编号)使用单独的列。嘿,谢谢!:)我希望我能给你们两个一个绿色的记号。
Private Sub CommandButton1_Click()
 Year_1 = 2017 'integer
 Day_1 = 1 'integer
 Date_from_userform = DateSerial(Year_1, ComboBox1.ListIndex + 1, Day_1)
End Sub

Private Sub UserForm_Initialize()
 For i = 1 To 12
  ComboBox1.AddItem Format(DateSerial(Year(Date), i, 1), "mmmm")
 Next
End Sub