我尝试了几种方法,但未能使用Excel VBA Userform ComboBox来处理宏。 基本上我们的数据记录器没有本地方式来提取数据并将其放入报告中。记录的时间始终为UTC,格式为日/小时/分钟/秒。我的Userform提取日志数据,进行调整,然后作为报告输出到Word。
我一直在尝试制作一个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 :(得分: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