我正在尝试创建一个宏来查看某一行中单元格的小时值,然后根据我的if,then,else条件对它们进行排序,并根据这个循环我的列生成结果。
因此,如果小时是< = 15且> = 11,那么结果是"午餐"。
我对VBA很新,但到目前为止我已经有了这个:
Sub Time_Sorter()
Dim i As Long
Dim result As String
For i = 1 To Rows.count
If Hour("J" & i) <= 15 And Hour("J" & i) >= 11 Then
result = "Lunch"
ElseIf Hour("J" & i) <= 16 And Hour("J" & i) >= 17 Then
result = "Pre-Dinner"
ElseIf Hour("J" & i) <= 18 And Hour("J" & i) >= 21 Then
result = "Dinner"
ElseIf Hour("J" & i) <= 22 And Hour("J" & i) >= 23 Then
result = "Late Night"
Else
result = "Other"
End If
Next i
End Sub
我一直收到消息
运行时错误&#39; 13&#39;:类型不匹配
而且我不确定如何修复它。
非常感谢任何见解!
答案 0 :(得分:0)
我认为您正在使用下面的代码。
Iv'e用构造的ElseIf
替换了你的多个Select Case
。
代码
Sub Time_Sorter()
Dim i As Long
Dim result As String
' loop from the first row until last row with data in column ""j"
For i = 1 To Cells(Rows.Count, "J").End(xlUp).Row
Select Case Hour(Range("J" & i).Value)
Case 11 To 15
result = "Lunch"
Case 16 To 17
result = "Pre-Dinner"
Case 18 To 21
result = "Dinner"
Case 22 To 23
result = "Late Night"
Case Else
result = "Other"
End Select
Next i
End Sub