【发布时间】:2020-06-25 13:06:29
【问题描述】:
我正在创建的 vba 代码有问题。我有一个 excel 文件,在前 3 列中我有一些信息,例如零件/编号等,请求的数量。然后在其他列的同一张表中,我有更多数据,其中我可能具有或可能不具有与第一列中相同的部件/编号,以及更多信息。这个想法是在第一列中逐个零件/编号,并检查它是否存在于最后一列(或数据集)中,并检查第二个数据集是否满足第一个数据集的数量要求,如果不要像在第二个数据集中那样向下移动到下一行,因为它们具有不同的价格、项目等,可能存在相同零件/编号的重复项。所以想法是,如果我在第二个数据集中有所需的数量首先,将这些值复制到不同的单元格中并创建一个总和,这样我就会知道最后的总数。我做了一些编码,但我遇到了错误,因为我对 vba 很陌生。任何帮助将不胜感激。谢谢。
A 列是第一个数据集的部分/编号,C 列是需要/请求的数量 AP 列是第二个数据集的零件/编号,AQ 是可用数量
Sub ExitFor_Loop()
Dim i, j, qty As Integer
Dim mySum As Double
mySum = 0
For i = 2 To 374
qty = Range("C" & i).Value
For j = 2 To 13672
If Range("A" & i).Value = Range("AP" & j).Value Then
Do
If qty > Range("AQ" & j).Value Then
Range("BC" & j).Value = Range("A" & i).Value
Range("BD" & j).Value = Range("AT" & j).Value
Range("BE" & j).Value = Range("AQ" & j).Value
Range("BF" & j).Value = Range("AV" & j).Value
Range("BG" & j).Value = Range("AW" & j).Value
Range("BH" & j).Value = Range("AX" & j).Value
Range("BI" & j).Value = Range("AY" & j).Value
Range("BJ" & j).Value = Range("AZ" & j).Value
mySum = mySum + Range("AQ" & j).Value * Range("AV" & j).Value
qty = qty - Range("AQ" & j).Value
Else
Range("BC" & j).Value = Range("A" & i).Value
Range("BD" & j).Value = Range("AT" & j).Value
Range("BE" & j).Value = Range("AQ" & j).Value
Range("BF" & j).Value = Range("AV" & j).Value
Range("BG" & j).Value = Range("AW" & j).Value
Range("BH" & j).Value = Range("AX" & j).Value
Range("BI" & j).Value = Range("AY" & j).Value
Range("BJ" & j).Value = Range("AZ" & j).Value
mySum = mySum + Range("AQ" & j).Value * Range("AV" & j).Value
End If
Loop While (qty > Range("AQ" & j).Value And ("A" & i).Value = Range("AP" & j).Value)
Next j
Next i
End Sub
【问题讨论】:
-
你也应该知道
Dim i, j, qty As Integer这个语句只是将qty声明为Integer类型变量。i和j被声明为Variant类型。在 vba 中,您需要独立且明确地声明每个变量。Dim i as Integer, j as Integer, qty as Integer -
mySum 要写入哪一列?