VBA EXCEL为表达式设置两个变量的多个嵌套FOR循环
好的,我已经做了大量的搜索,找到了一些,玩了一些,很少。我似乎无法使这些循环完全工作,我可以部分地工作,但不能全部工作。正如第一个循环工作正常,然后它就不稳定了VBA EXCEL为表达式设置两个变量的多个嵌套FOR循环,vba,loops,excel,for-loop,nested,Vba,Loops,Excel,For Loop,Nested,好的,我已经做了大量的搜索,找到了一些,玩了一些,很少。我似乎无法使这些循环完全工作,我可以部分地工作,但不能全部工作。正如第一个循环工作正常,然后它就不稳定了 T是表达式输出的目标T.Value=time1-time2 Y是一个设置的时间和日期,不会改变=time1 X是时间和日期,必须从与相应的y相同列中的范围中提取x=时间2 我已上载工作簿的相应部分 https://docs.google.com/open?id=0BzGnV1BGYQbvMERWU3VkdGFTQS1tYXpXcU1Mc
T
是表达式输出的目标T.Value=time1-time2
Y
是一个设置的时间和日期,不会改变=time1
X
是时间和日期,必须从与相应的y
相同列中的范围中提取<代码>x=时间2
我已上载工作簿的相应部分
https://docs.google.com/open?id=0BzGnV1BGYQbvMERWU3VkdGFTQS1tYXpXcU1Mc3lmUQ
我已经玩过条件出口重新排列for循环。我甚至考虑过尝试goto,直到我注意到一提到它就产生了一大堆尸体
我乐于接受并感谢任何建议或指导。我注意到一些语言有退出和继续选项,但VB似乎没有
这是我的循环,我已经把我在试图让它工作时弄得乱七八糟的东西去掉了
Sub stituterangers()
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date
For Each t In range("d7:cv7")
For Each x In range("d8:cv11")
If x > 0 Then time2 = x
For Each y In range("d2:cv2")
time1 = y
t.Value = time1 - time2
t = 0
Next y
Next x
Next t
End Sub
Sub stituterangersNEW()
Dim t As range
Dim x As range
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date
On Error Resume Next
'Looping through each of our output cells.
For Each t In range("d7:cv7")
For Each y In range("d2:cv2")
If t.Column = y.Column Then
time1 = y.Value
If y = 0 Then Exit Sub
End If
For Each x In range("d8:cv11")
'Check to see if our dep time corresponds to
'the matching column in our output
If t.Column = x.Column Then
If x > 0 Then
time2 = x.Value
t.Value = time1 - time2
Exit For
End If
End If
Next x
Next y
Next t
End Sub
我目前无法访问您的google文档文件,但您的代码中存在一些问题,我将在回答时尝试解决这些问题
Sub stituterangersNEW()
Dim t As Range
Dim x As Range
Dim dify As Boolean
Dim difx As Boolean
Dim time2 As Date
Dim time1 As Date
'You said time1 doesn't change, so I left it in a singe cell.
'If that is not correct, you will have to play with this some more.
time1 = Range("A6").Value
'Looping through each of our output cells.
For Each t In Range("B7:E9") 'Change these to match your real ranges.
'Looping through each departure date/time.
'(Only one row in your example. This can be adjusted if needed.)
For Each x In Range("B2:E2") 'Change these to match your real ranges.
'Check to see if our dep time corresponds to
'the matching column in our output
If t.Column = x.Column Then
'If it does, then check to see what our time value is
If x > 0 Then
time2 = x.Value
'Apply the change to the output cell.
t.Value = time1 - time2
'Exit out of this loop and move to the next output cell.
Exit For
End If
End If
'If the columns don't match, or the x value is not a time
'then we'll move to the next dep time (x)
Next x
Next t
End Sub
编辑 我更改了您的工作表以供使用(请参见上文中的新Sub)。这可能不能直接满足您的需求,但希望它能展示我认为您想要做的事情背后的理念。请记住,此代码并没有遵循我建议的所有编码最佳实践(例如,验证时间实际上是一个时间,而不是一些随机的其他数据类型) 使用sub,我得到了以下输出:
A B C D E
7 Friday 1272:00:00 1272:00:00 1272:00:00 1271:30:00
8 Saturday 1272:00:00 1272:00:00 1272:00:00 1271:30:00
9 Thursday 1272:00:00 1272:00:00 1272:00:00 1271:30:00
谢谢你的回复。这有助于我了解有条件退出是如何与我的想法协同工作的,也许我在这方面的方法是不正确的。在我无法使循环同步之前,我用这些代码胡乱处理过。我将使用D列作为D列中的一个示例,我有一个单元格用于输出,它将=t 1单元格用于y,时间X由4个单元格d8、d9、d10、d11组成,这些单元格对于D列中的循环只有一个值。本质上,我需要…在D列中设置相应的y值,在4个单元格中搜索X的值,计算T。一旦计算了T,我需要将整个内容向上移动到下一列,当前为D列到CV列。我的方法对吗?我认为,使用这种方法,x扫描列,然后是行,而不是行,然后是下一列,也会成为一个问题。自从大约98,99年以来,我没有做过很多编程,那是在qbasic中。所以我winging@PCGIZMO
啊,我从QB开始的时候是::-)代码>您能否在问题中发布您的数据示例以及您希望从中获得的输出?我想这会对我有所帮助,因为我还不能100%确定我知道你想要得到什么。您是否正在尝试将d单元的值设置为完全相同(y值)?我将看看我能做什么,因为我没有代表添加照片/屏幕捕获。谷歌文档链接有这段代码的工作手册。我的工作防火墙不允许我查看,对不起。除了屏幕截图,你能从xls(x)中复制并粘贴几行来粘贴到这里吗?
A B C D E
7 Friday 1272:00:00 1272:00:00 1272:00:00 1271:30:00
8 Saturday 1272:00:00 1272:00:00 1272:00:00 1271:30:00
9 Thursday 1272:00:00 1272:00:00 1272:00:00 1271:30:00