1
如何將時間與時區分開並以格式「yyyy-m hh:mm:ss」放置時間。查找「Time」列,創建另外兩列:「Time *」和「Time_Zone」。 我適應這個代碼,但有些錯誤發生,我把「上的錯誤繼續下一步」如何將時間與時區分開並將時間格式設置爲「yyyy-m hh:mm:ss」?
For Each ws In Worksheets
For i = 1 To ws.Columns.Count
If ws.Cells(1, i) = "Hour" Then
Set s = ws.Cells(1, i)
LC = s.Column
ws.Columns(LC + 1).Insert
ws.Columns(LC).Copy
ws.Cells(1, LC + 1).PasteSpecial Paste:=xlPasteValues
ws.Cells(1, LC + 1).Value = "Time*"
Exit For
End If
Next i
For i = 1 To ws.Columns.Count
If ws.Cells(1, i) = "Time*" Then
ColLetr = Split(Cells(1, i).Address, "$")(1)
y = i
Exit For
End If
Next i
If ColLetr <> "" Then
lastRow = ws.Cells(Rows.Count, y).End(xlUp).Row
For Each cell In ws.Range(ColLetr & "3:" & ColLetr & lastRow)
If InStr(cell.Value, "/") <> 0 Then
cell.Value = RegexReplace(cell.Value, _
"(\d{2})\/(\d{2})\/(\d{4})", "$3-$2-$1")
End If
cell.NumberFormat = "yyyy-mm-dd hh:mm:ss;@"
If cell.Value <> "" Then
cell.Value = Left(cell.Value, 19)
End If
Next
End If
For i = 1 To ws.Columns.Count
If ws.Cells(1, i) = "Hour" Then
Set s = ws.Cells(1, i)
LC = s.Column
ws.Columns(LC + 2).Insert
ws.Columns(LC).Copy
ws.Cells(1, LC + 2).PasteSpecial Paste:=xlPasteValues
ws.Cells(1, LC + 2).Value = "Time_Zone"
Exit For
End If
Next i
For i = 1 To ws.Columns.Count
If ws.Cells(1, i) = "Time_Zone" Then
ColLetr = Split(Cells(1, i).Address, "$")(1)
y = i
Exit For
End If
Next i
If ColLetr <> "" Then
lastRow = ws.Cells(Rows.Count, y).End(xlUp).Row
For Each c In ws.Range(ColLetr & "3:" & ColLetr & lastRow)
If c.Value <> "" Then
On Error Resume Next
c.Value = Right(c.Value, Len(c.Value) - 20)
End If
Next
End If
Next
Application.ScreenUpdating = False
End Sub
Function RegexReplace(ByVal text As String, _
ByVal replace_what As String, _
ByVal replace_with As String) As String
Application.ScreenUpdating = False
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = replace_what
RE.Global = True
RegexReplace = RE.Replace(text, replace_with)
Application.ScreenUpdating = True
End Function
謝謝你的工作,這對我很有幫助。 –