【问题标题】:vba code not taking correct value of a cell from filevba代码没有从文件中获取正确的单元格值
【发布时间】: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

这是我从中单击按钮(或 ThisWorkbook)的文件

这是在最后一行数据上运行时的永久文件:

请注意 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() 没有工作表引用,也许这就是它抛出随机结果的原因。我会发布答案并感谢您。谢谢!

标签: vba excel


【解决方案1】:

1)

Cells(i, 1) = i - 4

正如它所写的那样,它指的是perporig.Cells(i, 1) 这是你想要的吗?

2)

perporig.Sheets("perpetual").Range("A3:J" & RowLast).AutoFilter Field:=1, Criteria1:=filterCriteria

将从第 3 行过滤,而第 4 行有标题,第 5 行向下有数据

改成

 perporig.Sheets("perpetual").Range("A4:J" & RowLast).AutoFilter Field:=1, Criteria1:=filterCriteria

3)

你认为Counter 在做什么?不一定只计算可见行

【讨论】:

  • 1.) 这很奇怪,您所说的是正确的,但我在 ThisWorkbook 中的 Sr. No. 列已正确填充。但你是对的,我会做出改变。
  • 2.) 永久工作表是 perporig 工作表,第二张图片,第一张图片是 ThisWorkbook。 perporig 表确实从第 3 行开始标题,从第 4 行开始数据。
  • 3.) 如果该项目根本不在 perporig 文件中,则计数器用于不引发错误并通过在我的工作簿中输入“Not on perpetual”使其可见。那必须以不同的方式处理。我不喜欢使用“on error resume next”,除非我确信我已经处理了所有可能的异常。
  • 2) 我明白了。你是对的 3)仍然无法掌握 Counter 变量的使用:假设如何防止错误以及什么类型/由什么语句抛出?
  • 当过滤器没有结果时,我得到空白行并且我曾经得到错误,我忘记了究竟是什么错误,因此我这样做了。另外,我希望用户知道在文件中根本找不到该值。我可以说这个变量没有正确命名,它并没有真正计算任何东西。这只是自动过滤器给出的结果行数。如果该值为零,则用户会看到 Not in perpetual 作为输出。
【解决方案2】:

感谢findwindow,我找到了答案。 .cells(cells()) 部分没有正确的内部 cells() 工作表引用:

代替

        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

我用过这个:

            With perporig.Sheets("perpetual")
                x = .Cells(.Cells(RowLast + 1, 1).End(xlUp).Row, 4).Value
                MsgBox x, vbOKOnly, .Cells(RowLast + 1, 1).End(xlUp).Row
            End With

它奏效了。
感谢您的帮助!

【讨论】:

  • 所以“永久”工作簿不止一张,不是吗?
猜你喜欢
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
相关资源
最近更新 更多