gpt4 book ai didi

excel - 我的 excel 文件是德国(德语)格式,想更改为英语(英国)格式

转载 作者:行者123 更新时间:2023-12-04 22:19:04 27 4
gpt4 key购买 nike

我已经尝试了多个代码,例如这样
enter image description here

Sub DateFixer()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
For Each r In Selection
v = r.Text
r.Clear
r.NumberFormat = "dd/mm/yyyy hh:mm:ss"
r.Value = DateSerial(Mid(v, 7, 4), Mid(v, 4, 2), Left(v, 2)) + TimeSerial(Mid(v, 12, 2), Mid(v, 15, 2), Right(v, 2))
Next r
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
和这个
Application.ActiveWorkbook.Worksheets("data").Range("A1:A3").NumberFormat = "dd/mm/yyyy hh:mm:ss am/pm
"
和这个
Sub changeformat()

Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox lastrow
For i = 2 To lastrow
Cells(i, 2).NumberFormat = ("dd/mm/yyyy hh:mm:ss am/pm")
Next i

End Sub
但它仍然没有将我的 DateTime 更改为 9/12/20 09:28:00 am。我可以知道我做错了什么吗?
enter image description here

最佳答案

您可以将每个部分拆分并强制它们成为日期。然后总结再改格式:
enter image description here

Sub changeformat()

Dim lastrow As Long
Dim i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow
Cells(i, 1).Value = CDate(Replace(Split(Cells(i, 1), " ")(0), ".", "/")) + CDate(Split(Cells(i, 1), " ")(1))
Cells(i, 1).NumberFormat = ("dd/mm/yyyy hh:mm:ss am/pm")
Next i

End Sub
执行代码后,我得到:
enter image description here
注意值 9.12.20 09:27:60无法转换,因为 09:27:60 不是 有效时间。秒总是从 00 到 59,而不是从 1 到 60。所以实际上 09:27:60应该是 09:28:00恐怕您需要为这种情况编写异常代码。

关于excel - 我的 excel 文件是德国(德语)格式,想更改为英语(英国)格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65992487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com