Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA循环检查两个范围的条件_Vba_Excel_Nested Loops - Fatal编程技术网

VBA循环检查两个范围的条件

VBA循环检查两个范围的条件,vba,excel,nested-loops,Vba,Excel,Nested Loops,我的工作模拟器有问题。 我正在模拟送货服务(我在一家快餐公司工作) 我需要一个宏,通过循环检查几个条件 它首先必须检查订单时间是否在1秒的间隔内(该间隔也循环增加1秒,这是最外层的循环),以及订单是否处于“保留”状态或已经交付 Sub loop1() Application.ScreenUpdating = False 'Timer starts Dim StartTime As Double Dim MinutesElapsed As String StartTime = Timer

我的工作模拟器有问题。 我正在模拟送货服务(我在一家快餐公司工作)

我需要一个宏,通过循环检查几个条件

它首先必须检查订单时间是否在1秒的间隔内(该间隔也循环增加1秒,这是最外层的循环),以及订单是否处于“保留”状态或已经交付

Sub loop1()


Application.ScreenUpdating = False

'Timer starts
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer

'Defines last row for both ranges
Sheets("Reloj").Select
Dim Lastrow As Long
Lastrow = ActiveSheet.Range("K" & Rows.Count).End(xlUp).Row
Sheets("Embajadores").Select
Dim Lastrow2 As Long
Lastrow2 = ActiveSheet.Range("f" & Rows.Count).End(xlUp).Row


Dim i As Integer
Dim rng As Range, pedido As Range
Set rng = (Worksheets("Reloj").Range("K12:K" & Lastrow))

Dim embajadores As Range, cell As Range
Set embajadores = (Worksheets("Embajadores").Range("f19:f" & Lastrow))



Sheets("Reloj").Select
'One second increment to this cell, which will begin at 13:00:00 for 
'little more than an hour.
For i = 1 To 3780
Cells(3, 3) = Cells(3, 3) + TimeSerial(0, 0, 1)
ActiveSheet.Calculate

'Start looping for each order 
        For Each pedido In rng
'Start looping for each delivery agent
            For Each cell In embajadores
            Sheets("Reloj").Select
'Check if the order time is between the two times (cells 3,3 is current time
'and cells 3,4 is one second more than 3,3)
            If pedido.Value >= Cells(3, 3) And pedido.Value < Cells(3, 4) And pedido.Offset(0, 1) = "Waiting" Then
'Checks if the order is coming in from the same restaurant 
                If pedido.Offset(0, 2) = cell.Offset(0, -1) Then
'Checks if the order time is greater than or equal than the delivery agent availability time
                        If pedido.Value >= cell.Value Then
                            'Changes the status to delivered
                                pedido.Offset(0, 1) = "Delivered"
                                Worksheets("Reloj").Calculate
                             'Changes the agent availability time to the delivery time of the specific order
                                cell.Value = pedido.Offset(0, 9)
                             'Adds one to the order counter
                                cell.Offset(0, 1) = cell.Offset(0, 1) + 1

                       End If
              End If
          End If
       Next cell
    Next pedido


Next i

'Finishes timer and shows MsgBox
    MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "The simulation ran in " & MinutesElapsed & " minutes", vbInformation

    Application.ScreenUpdating = True

End Sub
然后它检查订单和代理是否来自同一家餐厅。 如果这一点成立,那么它必须检查订单时间是否大于或等于送货代理可用的时间

如果所有这些都成立,那么它会将订单状态更改为“已送达”,也会将送达代理的时间更改为他返回餐厅的时间。最后,它为每个交付代理添加一个计数器,以查看每个代理交付了多少

Sub loop1()


Application.ScreenUpdating = False

'Timer starts
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer

'Defines last row for both ranges
Sheets("Reloj").Select
Dim Lastrow As Long
Lastrow = ActiveSheet.Range("K" & Rows.Count).End(xlUp).Row
Sheets("Embajadores").Select
Dim Lastrow2 As Long
Lastrow2 = ActiveSheet.Range("f" & Rows.Count).End(xlUp).Row


Dim i As Integer
Dim rng As Range, pedido As Range
Set rng = (Worksheets("Reloj").Range("K12:K" & Lastrow))

Dim embajadores As Range, cell As Range
Set embajadores = (Worksheets("Embajadores").Range("f19:f" & Lastrow))



Sheets("Reloj").Select
'One second increment to this cell, which will begin at 13:00:00 for 
'little more than an hour.
For i = 1 To 3780
Cells(3, 3) = Cells(3, 3) + TimeSerial(0, 0, 1)
ActiveSheet.Calculate

'Start looping for each order 
        For Each pedido In rng
'Start looping for each delivery agent
            For Each cell In embajadores
            Sheets("Reloj").Select
'Check if the order time is between the two times (cells 3,3 is current time
'and cells 3,4 is one second more than 3,3)
            If pedido.Value >= Cells(3, 3) And pedido.Value < Cells(3, 4) And pedido.Offset(0, 1) = "Waiting" Then
'Checks if the order is coming in from the same restaurant 
                If pedido.Offset(0, 2) = cell.Offset(0, -1) Then
'Checks if the order time is greater than or equal than the delivery agent availability time
                        If pedido.Value >= cell.Value Then
                            'Changes the status to delivered
                                pedido.Offset(0, 1) = "Delivered"
                                Worksheets("Reloj").Calculate
                             'Changes the agent availability time to the delivery time of the specific order
                                cell.Value = pedido.Offset(0, 9)
                             'Adds one to the order counter
                                cell.Offset(0, 1) = cell.Offset(0, 1) + 1

                       End If
              End If
          End If
       Next cell
    Next pedido


Next i

'Finishes timer and shows MsgBox
    MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "The simulation ran in " & MinutesElapsed & " minutes", vbInformation

    Application.ScreenUpdating = True

End Sub
当我尝试这个方法时,它不起作用,因为它没有首先循环通过每一行,它只是在第一次不满足条件时退出到Else

请帮忙,我明白我的语法很差,因为我不是一个程序员,而且英语也不是我的第一语言

以下是我的表格示例:

rng

the first column is my order time
second column is status
third column is restaurant
first column restaurant, 
second column is availability time, 
third column is order counter

大使馆:

the first column is my order time
second column is status
third column is restaurant
first column restaurant, 
second column is availability time, 
third column is order counter

请帮帮我,我知道我快拿到了,但我不能拿到最后一部分

-----------------------编辑-------------------------------------------------------------------------

我现在的问题就是这样,我将用一个例子来解释: 让我们想象一下,餐厅1的所有送货代理都在街上,他们都在下午1:15回来。然而,该餐厅的订单在下午1:13到达

现在,它将跳过该事务,因为它们都没有达到条件(我无法发布该事务的图像)

如您所见,它跳过了餐厅Zona 4的所有3笔交易,因为它没有找到送货代理。我想要的是我希望这些订单被延迟(我想在时间上增加一秒钟,直到一个送货代理达到条件)

(无法发布此的图像)

所有这些代理都是在下订单后进来的,因此他们没有被分配订单

我知道现在我没有发生这种情况的
Else
条件,但我不知道如何做到这一点。请帮忙。还有,如果我把

Else
pedido.Value = Cells(3,3).Value + TimeSerial(0, 0, 1)

在最里面的If中,当它发现第一个观察结果不符合条件时,它将使我脱离循环,即使在更深处可能有可用的代理。

一些快速的胜利:移动
Sheets(“Reloj”)。在循环之外选择
,它永远不会改变。是否已关闭工作表计算?如果没有,请删除所有
.Calculate
行。将
块一起用于
pedido
@Comintern Hi。谢谢你的第一个提示。我确实关闭了工作表计算,这就是我使用
的原因。计算
至于最后一个技巧,你能告诉我如何使用
块吗?这对我有什么帮助呢?大量关于使用
的在线信息。但您可以先用pedido
块将您的
块包装在
中,然后用
块将
pedidio.
的所有引用重新放在该块中block@dbmitch我已经按照你说的做了,循环正在工作,所以谢谢你。但是,我仍然需要帮助以时钟时间最终替换订单时间。我喜欢用纯VBA编写模拟(而不是不断地向电子表格中写入值和读取值)。将电子表格用作数据结构是不必要的慢。在模拟开始时从工作表中获取值,完成后将输出转储到工作表中。这样的方法不仅可能更快,而且是一种更干净的设计,允许您关注模拟的逻辑。不幸的是,这需要大量重写代码。一些捷径:移动
工作表(“Reloj”)。选择循环外的
——它永远不会改变。是否已关闭工作表计算?如果没有,请删除所有
.Calculate
行。将
块一起用于
pedido
@Comintern Hi。谢谢你的第一个提示。我确实关闭了工作表计算,这就是我使用
的原因。计算
至于最后一个技巧,你能告诉我如何使用
块吗?这对我有什么帮助呢?大量关于使用
的在线信息。但您可以先用pedido
块将您的
块包装在
中,然后用
块将
pedidio.
的所有引用重新放在该块中block@dbmitch我已经按照你说的做了,循环正在工作,所以谢谢你。但是,我仍然需要帮助以时钟时间最终替换订单时间。我喜欢用纯VBA编写模拟(而不是不断地向电子表格中写入值和读取值)。将电子表格用作数据结构是不必要的慢。在模拟开始时从工作表中获取值,完成后将输出转储到工作表中。这样的方法不仅可能更快,而且是一种更干净的设计,允许您关注模拟的逻辑。不幸的是,这需要对代码进行大量重写。