【发布时间】:2017-11-21 18:01:55
【问题描述】:
我在这个项目中使用 Excel VBA。该项目的目标是拥有一个显示许多列的工作表。每列标题都有一个项目代码(用于勺子)和库存中勺子的数量。这部分是创建和功能的。
然后我希望工作簿搜索未结订单列表,获取在未结订单上创建产品所需的项目 ID,并将其放入先前创建的项目列中。
以下代码是我目前所拥有的
For j = 2 to 264
For p =2 to 300
'If the item on the order matches the item on the material required sheet, then the variables are set and it starts the inner loop
If Sheets("OOR").Cells(j, 5).Value = Sheets("BOM").Cells(p, 1).Value then
FgScoQty = Sheets("OOR").Cells(j, 10).Value
ItemScoop = Sheets("BOM").Cells(p, 4).Value
FgItem = Sheets("BOM").Cells(p, 1).Value
'If they match, it then looks on the sheet with the columns to put this information in the next blank row of the correct column
For x = 1 to 258
If ItemScoop = Sheets("Sheet5").Cells(1, x).Value Then
lastRow = Sheets("Sheets5").Cells(Rows.Count, x).End(xlUp).Offset(1, 0).Row
Sheets("Sheet5").Cells(lastRow, x).Value = fgItem
Sheets("Sheet5").Cells(lastRow, x).Offset(0, 1).Value = fgScoQty
End If
Next x
End If
Next p
Next j
第一次完美运行
在那之后我遇到了一个问题
If ItemScoop = Sheets("Sheet5").Cells(1, x).Value Then
我不知道为什么。我相信在满足条件后我需要退出内部循环(循环 x)。我在网上做了很多搜索,因为这似乎是一个相当普遍的问题,但我找不到任何有效的方法。
当我尝试Exit For 时,它似乎将我从所有循环中移除。
我想让它用变量填充单元格,然后回到开头看下一个j。
【问题讨论】:
-
就在
End If之前放Exit For -
你把
Exit For放在哪里,让它退出所有循环?这不是Exit For所做的——它只会退出它所写入的循环体。 -
FWIW 如果将循环替换为
.Find,您将获得性能提升