Vba如何让if向上循环?

图中红框里,如果if值为假,则让x减小1,重新执行if,直到值为真,请问应该怎么修改

Sub zhao()
Dim x As Integer
Dim y As Integer
For y = 100 To 7 Step -1
For x = 100 To 7 Step -1
If Cells(x, 1).Value = 1 Then
Cells(y, 5).Value = Cells(x, 3).Value
Else
End If
Next
Next
End Sub
以下为新回答:
Sub zhao()
Dim x As Integer
Dim y As Integer
For y = 100 To 7 Step -1
For x = y To 7 Step -1
If Cells(x, 1).Value = 1 Then
Cells(y, 5).Value = Cells(x, 3).Value
Exit For
Else
End If
Next
Next
End Sub
不知道这个能不能符合你的要求?
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-07-06
这种情况还是改用当循环吧do while
第2个回答  2022-07-07
Sub 比对数据()
Dim x As Integer
Dim y As Integer
For y = 100 To 7 Step -1
For x = 100 To 7 Step -1
If Cells(x, 1).Value = 1 Then
Cells(y, 5).Value = Cells(x, 3).Value
Exit For
End If
Next x
Next y
End Sub
把这个运行一下,就是需要的结果
或者,你的变量Y是多余的,你是想把与第一列单元格为1的在同行5列的数据改成3列的,下面这个就行
Sub 比对()
Dim x As Integer
For x = 100 To 7 Step -1
If Cells(x, 1).Value = 1 Then
Cells(x, 5) = Cells(x, 3).Value
End If
Next x
End Sub
相似回答