autofilter vba之后的列总和

我在尝试着:
– 打开一个文件"perpetual.xlsx"
– 根据标准筛选列
– 取另一列的总和,并将其input到另一列的原始文件中。 我的原始文件如下图所示。
– 从第2步继续循环,直到原始文件的最后一行。

在这里输入图像说明 这是我的代码:

 Private Sub CommandButton1_Click() Dim RowLast As Long Dim tempLast As Long tempLast = ThisWorkbook.Sheets("combine BOMs").Cells(Rows.Count, "B").End(xlUp).Row Dim perporig As Workbook 'ThisWorkbook.Sheets("combine BOMs").Cells(1, 9).Value = here For i = 5 To tempLast Set perporig = Workbooks.Open("\\Etnfps02\vol1\DATA\Inventory\Daily tracking\perpetual.xlsx", UpdateLinks:=False, ReadOnly:=True) With perporig.Sheets("perpetual") .AutoFilterMode = False RowLast = .Cells(Rows.Count, "A").End(xlUp).Row .Range("A3:J" & RowLast).AutoFilter field:=1, criteria:=Range("B" & i).Value ThisWorkbook.Sheets("combine BOMs").Cells(i, 5).Value = Application.WorksheetFunction.Sum(Columns("H:H")) .AutoFilterMode = False End With perporig.Close savechanges:=False ThisWorkbook.Sheets("myperpetual").Activate Next i Cells(1, 9).Value = here End Sub 

代码只运行到第9行,即:Set perporig = ….之后,由于某种原因停止。 文件“perpetual.xlsx”打开,代码停止。 如果我删除第6行中的撇号,代码似乎并没有运行。

编辑代码:感谢您的build议。 不知道在哪里放我的代码,所以在这里。 再次,代码只运行到第7行,即:设置perporig = ….之后,由于某种原因停止。 文件“perpetual.xlsx”打开,代码停止。 如果我删除第6行中的撇号,代码似乎并没有运行。 请帮忙。

 Private Sub CommandButton1_Click() Dim RowLast As Long Dim tempLast As Long tempLast = ThisWorkbook.Sheets("combine BOMs").Cells(Rows.Count, "B").End(xlUp).Row Dim perporig As Workbook 'ThisWorkbook.Sheets("combine BOMs").Cells(1, 9).Value = "here" Set perporig = Workbooks.Open("\\Etnfps02\vol1\DATA\Inventory\Daily tracking\perpetual.xlsx", UpdateLinks:=False, ReadOnly:=True) For i = 5 To tempLast With perporig.Sheets("perpetual") .AutoFilterMode = False RowLast = .Cells(Rows.Count, "A").End(xlUp).Row .Range("A3:J" & RowLast).AutoFilter field:=1, criteria:=ThisWorkbook.Sheets("combine BOMs").Cells(i, 2).Value ThisWorkbook.Sheets("combine BOMs").Cells(i, 5).Value = Application.WorksheetFunction.Sum(Range("H:H").SpecialCells(xlCellTypeVisible)) .AutoFilterMode = False End With Next perporig.Close savechanges:=False ThisWorkbook.Sheets("combine BOMs").Activate Cells(1, 9).Value = "here" End Sub 

7号线不能运行的原因是因为你没有定义“这里”是什么。 如果你想把文本放在那个单元格中,你应该用=“here”结束这行,这样这个值就是一个string。

正如另一位评论者所说。 您不应该在循环中打开工作簿。 首先打开工作簿,然后打开书(perporig)进行循环操作。

最后得到它,这是一个简单的错误,除了其他人的build议,我有mentoend标准:=而不是criteria1:=
愚蠢的错误。