VBA: 用户窗体错误日期值更改 - 月为日,日为月
VBA: Userform Error date values change - months as days and days as months
我在用户表单中使用 calendar 在 Textbox1 中输入日期。它在用户窗体(格式 "dd/mm/yyyy")中工作正常 - 将日期直接填充到活动单元格都没有问题。
将数据从用户窗体传递到工作表时出现问题。日期格式保持 "dd/mm/yyyy" 但将日值更改为月份,将月份更改为日(3 月 1 日变为 1 月 3 日)。
ws.Cells(MyNR, 1) = tbFi.Value
ws.Cells(MyNR, 2) = tbNh.Value
ws.Cells(MyNR, 3) = tbTh.Value
ws.Cells(MyNR, 4) = cbN1.Value
ws.Cells(MyNR, 5) = cbPoc.Value
ws.Cells(MyNR, 6) = cbNa1.Value
ws.Cells(MyNR, 7) = cbPoca1.Value
ws.Cells(MyNR, 8) = cbNa2.Value
ws.Cells(MyNR, 9) = cbPoca2.Value
ws.Cells(MyNR, 10) = cbNa3.Value
ws.Cells(MyNR, 11) = cbPoca3.Value
ws.Cells(MyNR, 12) = cbNa4.Value
ws.Cells(MyNR, 13) = cbPoca4.Value
ws.Cells(MyNR, 14) = TextBox1.value
'ws.Cells(mynr, 14).NumberFormat = "dd/mm/yyyy" not working
ws.Cells(MyNR, 15) = TextBox2.value
'ws.Cells(mynr, 15).NumberFormat = "dd/mm/yyyy"
ws.Cells(MyNR, 16) = cbQac.Value
使用CDate
将文本转换为实际日期值:
ws.Cells(MyNR, 14).Value = CDate(TextBox1.value)
ws.Cells(MyNR, 15).Value = CDate(TextBox2.value)
我在用户表单中使用 calendar 在 Textbox1 中输入日期。它在用户窗体(格式 "dd/mm/yyyy")中工作正常 - 将日期直接填充到活动单元格都没有问题。
将数据从用户窗体传递到工作表时出现问题。日期格式保持 "dd/mm/yyyy" 但将日值更改为月份,将月份更改为日(3 月 1 日变为 1 月 3 日)。
ws.Cells(MyNR, 1) = tbFi.Value
ws.Cells(MyNR, 2) = tbNh.Value
ws.Cells(MyNR, 3) = tbTh.Value
ws.Cells(MyNR, 4) = cbN1.Value
ws.Cells(MyNR, 5) = cbPoc.Value
ws.Cells(MyNR, 6) = cbNa1.Value
ws.Cells(MyNR, 7) = cbPoca1.Value
ws.Cells(MyNR, 8) = cbNa2.Value
ws.Cells(MyNR, 9) = cbPoca2.Value
ws.Cells(MyNR, 10) = cbNa3.Value
ws.Cells(MyNR, 11) = cbPoca3.Value
ws.Cells(MyNR, 12) = cbNa4.Value
ws.Cells(MyNR, 13) = cbPoca4.Value
ws.Cells(MyNR, 14) = TextBox1.value
'ws.Cells(mynr, 14).NumberFormat = "dd/mm/yyyy" not working
ws.Cells(MyNR, 15) = TextBox2.value
'ws.Cells(mynr, 15).NumberFormat = "dd/mm/yyyy"
ws.Cells(MyNR, 16) = cbQac.Value
使用CDate
将文本转换为实际日期值:
ws.Cells(MyNR, 14).Value = CDate(TextBox1.value)
ws.Cells(MyNR, 15).Value = CDate(TextBox2.value)