我寫了一個程式,用來計算空倉(已驗證)、可能是空倉(未驗證)以及是否可以訪問(倉位鎖定)。 我正試圖從我的倉位轉換表中計算被鎖定的倉位,如果它們是 "真"(有20個是 "真"),那么它們就是被鎖定的,并將在我的倉位報告表中被計算出來。
我的問題是,在我的賓語報告表中,由于某種原因,他們似乎為每組計算了太多的數量(所有組加起來共23個,而不是20個)。一個組的例子是4個托盤,2.5英尺,2個箱子被鎖定(而不是1個)。
是否知道是什么原因導致我的代碼出現這種情況? 我是不是不小心在我的代碼中的某個地方添加了一個不應該添加的 1?
Sub getBinStatusArray()
計算 (False)
Dim dSH As 作業表
Dim brSH As 作業表
Dim bcSH As 作業表
Set dSH = ThisWorkbook.Sheets("data"/span>)
Set brSH = ThisWorkbook.Sheet("Bin Report")
Set bcSH = ThisWorkbook.Sheet("Bin Conversions")
Dim binLockCell As Byte, binType As String, binSize As Variant, binLocked As Boolean, b As Long, i As Long.
Dim dataArray() As Variant
Dim binIDArray As Variant
'創建空陣列單元格。
ReDim Preserve dataArray(1 To dSH. Range("A" & Rows.Count).End(xlUp).Row, 1 To 3)
'導航單元格。
With dSH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
dataArray = .Range(.Cells(lastrow, 1), .Cells(1, .Columns.Count).End(xlToLeft) ) .Value
結束 有
'Count Bin Conversion Cells[/span]。
With bcSH
Lastrow = .Cells(Rows.Count, 1).End(xlUp) .Row
For i = 2 To lastrow
.Range("E" & i).Value2 = Application.WorksheetFunction.CountIf(dSH.Range("A:A"), .Range("A" & i) .Value2)
Next i
End With[/span].
'Generate Bin Report With
With brSH
.Cells.ClearContents
.Range("H1").Value = "篩選輸入"。
.Range("B1").Value = "Bin型別"。
.Range("I1").Value = "Bin Type".
.Range("C1").Value = "垃圾箱高度"。
.Range("J1").Value = "Bin高度".
.Range("D1").Value = "驗證的"。
.Range("K1").Value = "Verified".
.Range("E1").Value = "Unverified"。
.Range("L1").Value = "Unverified"。
.Range("F1").Value = "Bins Locked"。
.Range("M1").Value = "Bins Locked"。
For i = 2 To lastrow
If bcSH.Range("E" & i).Value = 1 Or Application. WorksheetFunction.CountIfs(bcSH.Range("G"/span> & i), "true") 然后
binType = bcSH.Range("B"/span> & i).Value
binSize = bcSH.Range("C" & i).Value
binLocked = bcSH.Range("H"/span> & i).Value
If .Range("b2") = "" Then
.Range("b2").Value = bcSH.Range("B" & i) .Value
.Range("c2").Value = bcSH.Range("C" & i).Value
.Range("F2").Value2 = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
ElseIf .Range("b2") <> "" Then
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp) .Row
For b = 2 To lastrow 1
If brSH.Range("B" & b) = binType And brSH. 范圍("C"/span> & b) = binSize Then
brSH.Range("D"/span> & b) = brSH.Range("D"/span> & b) bcSH.Range("E"/span> & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G"/span> & i), "true")
brSH.Range("F"/span> & b) = binLockCell brSH.Range("F"/span> & b)
退出 對于來說
ElseIf b=lastrow Then
.Range("b" & b 1).Value = bcSH.Range("B" & i) .Value
.Range("c" & b 1).Value = bcSH.Range("c" & i) .Value
.Range("D" & b 1).Value = bcSH.Range("E" & i) .Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G"/span> & i), "true"/span>)
.Range("F"/span> & b 1) = binLockCell .Range("F"/span> & b 1)
結束 If
Next b
結束 若
End If
Next i
Range("b1").CurrentRegion.sort key1:=Range("b1"), order1:=xlAscending, _
key2:=Range("C1"/span>), order2:=xlAscending, Header:=xlYes
結束 與
計算 (True)
結束 子
uj5u.com熱心網友回復:
你正在回圈For b = 2 To lastrow 1,但是當b = lastrow時,即在回圈結束之前,添加了一個新行。所以在最后一次迭代時,當b = lastrow 1,它再次總結記錄。一個解決方法是使用一個標志。
ElseIf .Range("b2") <> " Then
Dim bExists: bExists = False
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp) .Row
' 增加現有的。
For b = 2 To lastrow
If brSH.Range("B" & b) = binType And brSH. 范圍("C"/span> & b) = binSize Then
brSH.Range("D"/span> & b) = brSH.Range("D"/span> & b) bcSH.Range("E"/span> & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G"/span> & i), "true")
brSH.Range("F"/span> & b) = binLockCell brSH.Range("F"/span> & b)
bExists = True[/span]。
退出 為
Next b
'或添加新行
If Not bExistsThen
.Range("b" & b 1).Value = bcSH.Range("B" & i) .Value
.Range("c" & b 1).Value = bcSH.Range("c" & i) .Value
.Range("D" & b 1).Value = bcSH.Range("E" & i) .Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G"/span> & i), "true"/span>)
.Range("F"/span> & b 1) = binLockCell .Range("F"/span> & b 1)
End If
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/323596.html
標籤:
