【发布时间】:2016-05-26 17:31:03
【问题描述】:
这是我的代码:
Dim RowLast As Long
Dim sunmLast As Long
Dim tempLast As Long
Dim filterCriteria As String
Dim perporig As Workbook
Dim x As String
tempLast = ThisWorkbook.Sheets("combine BOMs").Cells(Rows.Count, "E").End(xlUp).Row
Range("D5:G" & tempLast).ClearContents
Range("G5:G" & tempLast).Interior.ColorIndex = 0
tempLast = ThisWorkbook.Sheets("combine BOMs").Cells(Rows.Count, "A").End(xlUp).Row
Range("A5:A" & tempLast).ClearContents
tempLast = ThisWorkbook.Sheets("combine BOMs").Cells(Rows.Count, "B").End(xlUp).Row
'Perpetual
Set perporig = Workbooks.Open("\\Etnfps02\vol1\DATA\Inventory\Daily tracking\perpetual.xlsx", UpdateLinks:=False, ReadOnly:=True)
RowLast = perporig.Sheets("perpetual").Cells(Rows.Count, "A").End(xlUp).Row
perporig.Sheets("perpetual").Cells(3, 1) = "Part Number"
For i = 5 To tempLast
Cells(i, 1) = i - 4
perporig.Sheets("perpetual").AutoFilterMode = False
filterCriteria = ThisWorkbook.Sheets("combine BOMs").Range("B" & i).Value
perporig.Sheets("perpetual").Range("A3:J" & RowLast).AutoFilter Field:=1, Criteria1:=filterCriteria
Counter = perporig.Sheets("perpetual").Cells(RowLast + 1, 1).End(xlUp).Row
If Counter = 3 Then
Cells(i, 5).Value = "Not on perpetual"
Else
ThisWorkbook.Sheets("combine BOMs").Cells(i, 5).Value = WorksheetFunction.Sum(perporig.Sheets("perpetual").Range("H4:H" & RowLast).SpecialCells(xlCellTypeVisible))
x = perporig.Sheets("perpetual").Cells(Cells(RowLast + 1, 1).End(xlUp).Row, 4).Value
MsgBox x, vbOKOnly, perporig.Sheets("perpetual").Cells(RowLast + 1, 1).End(xlUp).Row
ThisWorkbook.Sheets("combine BOMs").Cells(i, 4).Value = x
End If
perporig.Sheets("perpetual").AutoFilterMode = False
Next
perporig.Close savechanges:=False
请注意 D9280 中的差异:它在永久文件中将库存类型显示为“P”,但在我的最终结果中显示为“B”,该结果出现在 ThisWorkbook 的单元格 D12 中。为了调试,我为每次获取所有行的值创建了一个 Msgbox 提示。对于每隔一行,它给出正确的值(“P”),但对于这一行,msgbox 显示“B”。 msgbox 的标题是行号,这表明它在获取值的同时采用了正确的行,只是我不知道为什么它采用了错误的值。我尝试过不同的数据源,它似乎经常在错误的地方出现“B”。
在代码中,就在该行的上方,我有一行来获取现有数量,它确实可以正确使用(我使用 xltypevisible 来粘贴该字段的值,但这只是因为我想要总和结果,这是我知道的唯一方法)。只有这个放养类型列会随机显示错误的值。
有什么想法吗?
谢谢!
【问题讨论】:
-
不知道为什么嵌套
cells而嵌套Cells(RowLast + 1?为什么是+1? -
@findwindow rowlast+1 因为我想粘贴在最后一行下方而不是最后一行。在观察到每个文件的最后一个条目一直消失后,我补充说。
-
但你不是在粘贴吗?我指的是您的
msgbox行,但请看用户在下面要说什么。 -
是的,msgbox 行中的 rowlast+1 只是为了安全起见,我不希望它尝试从隐藏行中进行 crtl+up。我不知道我是否正确,但它没有在 msgbox 行中抛出错误的结果..
-
@findwindow 我忘了回复您的嵌套单元格评论。你确实用你的评论把我引向了答案。我嵌套单元格的原因是内部单元格()给出了行号,我想要第一列的最后一个行号,然后向右移动并给我同一行第 4 列中的值。有时其他列有空数据,我不希望它影响我的结果。但是内部 cells() 没有工作表引用,也许这就是它抛出随机结果的原因。我会发布答案并感谢您。谢谢!