更新:我让代码做了我想做的事情(粘贴在下面),尽管有些人可能不同意我的方法(我会接受反馈,因为我是100%的新手)。
我正在尝试创建一个简单的VBA代码来运行一个带有if then语句的循环,-and如果语句为真,则停止循环。如果语句在结束之前从未为真,则当单元为IsEmpty()时停止循环。
这就是我到目前为止所拥有的。这显然是不正确的,而且它没有运行,但我希望这次尝试能帮助您理解我正在尝试做的事情。
Sub runoutDateLoop()
r = 8
Do Until IsEmpty(Cells(r, 1))
c = 24
Do Until Cells(r, c - 1).Value < 1 = True
If Cells(r, c).Value < 1 Then
Cells(r, 18).Value = Cells(7, c)
Else
Cells(r, 18).Value = Cells(7, c)
End If
c = c + 1
Loop
r = r + 1
Loop
End Sub我正在尝试创建一个循环,直到if语句为true。if then语句很简单-如果数字是负数,那么我们希望单元格r,18复制到单元格7,c。如果不是,则循环进行,直到有负数或如果从来没有负数,则直到单元格为空。我希望它对每一行都有一个double Do Until循环。
谁有什么好主意??
谢谢!
发布于 2018-08-07 07:58:50
要提前退出Do循环,请使用Exit Do。由于您有两个嵌套循环,因此需要一个标志来退出外部循环。
Sub RunoutDateLoop()
Dim r As Long, c As Long
Dim Found As Boolean
r = 8
Do Until IsEmpty(Cells(r, 1))
c = 24
Do Until IsEmpty(Cells(r, c))
If Cells(r, c).Value < 1 Then
Cells(r, 18).Value = Cells(7, c)
Found = True
Exit Do 'Exit the inner do
End If
c = c + 1
Loop
If Found Then Exit Do 'exit the outer do
r = r + 1
Loop
End Sub注意:我不会对循环中的逻辑做任何评论,它有点不清楚你的数据是什么,以及你需要的确切逻辑(例如,如果数字是负数对If Cells(r, c).Value < 1)
发布于 2018-08-07 08:20:56
有了help每个人的评论,这就是我所拥有的和我想要它做的工作。这可能有点令人困惑,因为我的数据非常复杂,如果没有实际看到电子表格,就很难理解。
Sub RunoutDateLoop()
Dim r As Long, c As Long
Dim Found As Boolean
r = 8
Do Until IsEmpty(Cells(r, 1))
c = 24
Do Until IsEmpty(Cells(r, c))
If Cells(r, c).Value < 1 Then
Cells(r, 18).Value = Cells(7, c)
Found = True
Exit Do 'Exit the inner do
Else
Cells(r, 18).Value = Cells(7, c)
End If
c = c + 1
Loop
r = r + 1
Loop
End Subhttps://stackoverflow.com/questions/51716955
复制相似问题