2016-01-23 106 views
0

我已經嘗試了幾種方法,但無法獲得Excel VBA Userform組合框以使用宏。 基本上我們的數據記錄器沒有提取數據並將其放入報告的原生方式。記錄的時間始終以UTC/Day/Hour/Minutes/Seconds格式顯示。我的用戶表單提取日誌數據,進行調整,然後作爲報告輸出到Word。Excel VBA時區調整器

我一直在努力使ComboBox,它允許用戶選擇一個時區到數據的UTC時間調整爲:即UTC + 9,日本等

大部分代碼的作品,但我不能讓它運行嵌套的ElseIf命令,代碼總是運行第一個如果不管然後完成。 我試過.Value和.ListIndex從ComboBox獲取值到一個字符串,但它只是不想知道。

任何幫助將不勝感激,因爲我只是不能讓它計算時間調整。 代碼示例是處理從其餘VBA項目中提取的時間調整的段。

Private Sub UserForm_Initialize() 

    Dim Local_Time As Range 
    Dim Time_Correction_Value As String 

    With ComboBox1 
     .AddItem "UTC+0"     ' List Index Value 0 
     .AddItem "UTC+1"     ' List Index Value 1 
     .AddItem "UTC+2"     ' List Index Value 2 
     .AddItem "UTC+3"     ' List Index Value 3 
     .AddItem "UTC+4"     ' List Index Value 4 
    End With 
End Sub 

Private Sub ComboBox1_Change() 

    If ComboBox1.ListIndex = 0 Then 
     Time_Correction_Value = 0 
    ElseIf ComboBox1.ListIndex = 1 Then 
     Time_Correction_Value = 1 
    ElseIf ComboBox1.ListIndex = 1 Then 
     Time_Correction_Value = 2 
    ElseIf ComboBox1.ListIndex = 2 Then 
     Time_Correction_Value = 3 
    ElseIf ComboBox1.ListIndex = 4 Then 
     Time_Correction_Value = 4 
    Else 
    End If 
End Sub 

Public Sub CORRECT_TIME_INDEX() 

    Columns("A:A").Select 
    Selection.Replace what:="**,", Replacement:="", LookAt:=xlPart, _ 
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
         ReplaceFormat:=False 
    Selection.NumberFormat = "h:mm" 

    For Each Local_Time In Intersect(Range("A:A"), ActiveSheet.UsedRange) 

     If Time_Correction_Value = 0 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(0, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 1 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(1, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 2 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(2, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 3 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(3, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 4 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(4, 0, 0) 
      End With 
     End If 
    Next 
    Columns("A:A").Select 
    Selection.NumberFormat = "h:mm" 
End Sub 

回答

0

你失去了價值:結合這兩個代碼,應該沒有問題。

Private Sub ComboBox1_Change() 

    If ComboBox1.ListIndex = 0 Then 
     Time_Correction_Value = 0 
    ElseIf ComboBox1.ListIndex = 1 Then 
     Time_Correction_Value = 1 
    ElseIf ComboBox1.ListIndex = 1 Then 
     Time_Correction_Value = 2 
    ElseIf ComboBox1.ListIndex = 2 Then 
     Time_Correction_Value = 3 
    ElseIf ComboBox1.ListIndex = 4 Then 
     Time_Correction_Value = 4 
    Else 
    End If 

    Columns("A:A").Replace what:="**,", Replacement:="", LookAt:=xlPart, _ 
          SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
          ReplaceFormat:=False 
    Columns("A:A").NumberFormat = "h:mm" 

    For Each Local_Time In Intersect(Range("A:A"), ActiveSheet.UsedRange) 

     If Time_Correction_Value = 0 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(0, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 1 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(1, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 2 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(2, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 3 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(3, 0, 0) 
      End With 
     ElseIf Time_Correction_Value = 4 Then 
      With Local_Time 
       .Value = .Value + TimeSerial(4, 0, 0) 
      End With 
     End If 
    Next 
    Columns("A:A").NumberFormat = "h:mm" 
End Sub