我有以下代码,它适用于列中的第一个值,但我需要修改代码,以便将if语句应用于列中的所有值。现在它将结果显示为列K中所有值的40 +,但我需要运行代码,以便首先评估K2,然后是K3,然后是K4等。请帮忙!
Sub UPTRange()
Dim UPT As Range, cell As Range, result As Range
Set UPT = Range("K2:K2642")
Set result = Range("L2:L2642")
For Each cell In UPT
If cell.Value >= 40 Then
result = "40 +"
ElseIf cell.Value = (30 <= 39) Then
result = "30 - 39"
ElseIf cell.Value = (20 <= 29) Then
result = "20 - 29"
ElseIf cell.Value = (10 <= 19) Then
result = "10 - 19"
ElseIf cell.Value = (2 <= 9) Then
result = "2 - 9"
ElseIf cell.Value = (0 <= 1) Then
result = "0 - 1"
Else: cell.Value = "Error"
End If
Next
For Each cell In result
Range("L2").Value = result
Next
End Sub
答案 0 :(得分:3)
If cell.Value = (30 <= 39) Then
与
相同If cell.Value = True Then
因为您正在评估表达式30 <= 39
,因为True
...
如果您想检查范围,那么您应该使用类似
的内容If cell.Value > 30 And cell.Value <= 39 Then
在result
中获得值之后,请执行以下操作:
cell.offset(0, 1).Value = result
将result
一个单元格放在cell
修改强>
Sub UPTRange()
Dim UPT As Range, cell As Range, result, v
Set UPT = ActiveSheet.Range("K2:K2642")
For Each cell In UPT
v=cell.value
If v >= 40 Then
result = "40 +"
ElseIf v > 30 And v <= 39) Then
result = "30 - 39"
ElseIf
'etc etc
Else
result = "Error"
End If
cell.Offset(0, 1).Value = result
Next
End Sub
答案 1 :(得分:0)
也许这个??
If cell.Value >= 40 Then
cell.Formula = "40 +"
ElseIf cell.Value >= 10 And cell.Value < 40 Then
cell.Formula = (Int(cell.Value / 10) * 10) & " - " & (Int(cell.Value / 10) * 10) + 9
ElseIf cell.Value > 1 And cell.Value < 10 Then
cell.Formula = "2 - 9"
Else
cell.Formula = "Error"
End If