2014-02-08 26 views
1

如何將時間與時區分開並以格式「yyyy-m hh:mm:ss」放置時間。查找「Time」列,創建另外兩列:「Time *」和「Time_Zone」。 我適應這個代碼,但有些錯誤發生,我把「上的錯誤繼續下一步」如何將時間與時區分開並將時間格式設置爲「yyyy-m hh:mm:ss」?

enter image description here

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 

回答

2

此代碼的工作對我來說:

Sub test() 
    Dim ws As Worksheet 
    Dim rngTime As Range 

    Dim cell As Range 
    Dim rngTarget As Range 
    Dim formatedTime As String 

    Application.ScreenUpdating = False 

    For Each ws In Worksheets 
     With ws 
      Set rngTime = .Range("1:1").Find(What:="Time", MatchCase:=False, LookAt:=xlWhole) 
      If Not rngTime Is Nothing Then 
       rngTime.Offset(, 1).Resize(, 2).EntireColumn.Insert 

       rngTime.Offset(, 1) = "Time*" 
       rngTime.Offset(, 2) = "Time_Zone" 

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

       Set rngTarget = .Range(.Cells(3, rngTime.Column + 1), .Cells(lastrow, rngTime.Column + 1)) 
       rngTarget.NumberFormat = "yyyy-mm-dd hh:mm:ss;@" 

       For Each cell In rngTarget 
        If InStr(cell.Offset(, -1), "/") <> 0 Then 
         formatedTime = RegexReplace(cell.Offset(, -1), _ 
          "(\d{2})\/(\d{2})\/(\d{4})", "$3-$2-$1") 
         cell = Trim(Left(formatedTime, 19)) 
         cell.Offset(, 1) = Trim(Mid(formatedTime, 20)) 
        End If 
       Next cell 
      End If 
     End With 
    Next ws 
    Application.ScreenUpdating = True 
End Sub 


Function RegexReplace(ByVal text As String, _ 
        ByVal replace_what As String, _ 
        ByVal replace_with As String) As String 

    Dim RE As Object 
    Set RE = CreateObject("vbscript.regexp") 

    RE.Pattern = replace_what 
    RE.Global = True 
    RegexReplace = RE.Replace(text, replace_with) 

End Function 

注意,在您正在使用的圖片Time標題列E,但在您的搜索Hour列:If ws.Cells(1, i) = "Hour" Then。我在我的代碼中使用了Time標題,您可以在行Set rngTime = .Range("1:1").Find(What:="Time", MatchCase:=False, LookAt:=xlWhole)中更改它。

結果:

enter image description here

+1

謝謝你的工作,這對我很有幫助。 –

相關問題