Excel VBA – 自动填充只在debugging过程中起作用

我正在尝试从另一个工作表上的工作表中的工作表复制单元格,并尝试将表中包含的公式自动填充到新创build的行中。

我的macros基本上从“需求”表中的有序表中取出所有可见的单元格,并将它们的值直接复制到“ITA&IO-EOTP”表中的表下。 表格自动展开,但并不是所有的公式都被应用到新的行(有些是)。 然后我从上面一行调用一个自动填充到新行。 当所有的新ID被复制,我sorting的一切。

问题是:当我逐行debuggingAutoFill的时候,当我启动没有断点的macros时,它会被简单的跳过…我不知道这是什么原因! 我已经花了整整一天的时间来试图弄明白这一切,这让我疯狂!

这是整个macros:

Public Sub InsertIntoITA() Dim y, availPos, startPos As Long Dim currCell, currSel As Range 'make it fast, plz Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ThisWorkbook.Activate Sheets("Demands").Select demandsImported = True 'check if demands were imported first If demandsImported Then 'select all new demands y = Range("A65536").End(xlUp).Row Set currSel = Range("A2:A" & y).SpecialCells(xlCellTypeVisible) 'get first position after the table in ITA sheet availPos = Sheets("ITA & IO-EOTP").Range("A65536").End(xlUp).Row + 1 startPos = availPos - 1 'check if there are demands at all If Not currSel.Count = 0 Then For Each currCell In currSel 'if cell contains #N/A (not what we want) it means the user hasn't attributed a new ID to the demand yet 'if it doesn't, proceed to copy the ID at the end of the table in ITA sheet If Not IsError(currCell) Then Sheets("ITA & IO-EOTP").Range("A" & availPos).Value = currCell.Value 'then autofill from the line above, to ensure formulas are applied Range("B" & availPos - 1 & ":P" & availPos - 1).AutoFill Destination:=Range("B" & availPos - 1 & ":P" & availPos) 'allow the table to automatically expand to include the new ID then 'increment available position availPos = availPos + 1 End If Next currCell 'Then order them Sheets("ITA & IO-EOTP").ListObjects("ITATable").Sort. _ SortFields.Clear Sheets("ITA & IO-EOTP").ListObjects("ITATable").Sort. _ SortFields.Add Key:=Range("ITATable[[#All],[ONE-IT ID]]"), SortOn:= _ xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With Sheets("ITA & IO-EOTP").ListObjects("ITATable").Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End If End If Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 

我希望你能看到问题在哪里

(另请注意:ITA工作表有一个macros,在激活时将.EnableCalculation设置为False,不知道它是否重要/有帮助)

 Private Sub Worksheet_Activate() Sheets("ITA & IO-EOTP").EnableCalculation = False End Sub 

好的,find罪魁祸首…我只需要在编译器进入For块之前激活ITA表单…

  Sheets("ITA & IO-EOTP").Activate For Each currCell In currSel 'if cell contains #N/A (not what we want) it means the user hasn't attributed a new ID to the demand yet 'if it doesn't, proceed to copy the ID at the end of the table in ITA sheet If Not IsError(currCell) Then Sheets("ITA & IO-EOTP").Range("A" & availPos).Value = currCell.Value 'then autofill from the line above, to ensure formulas are applied Range("B" & availPos - 1 & ":P" & availPos - 1).AutoFill Destination:=Range("B" & availPos - 1 & ":P" & availPos) 'increment available position availPos = availPos + 1 End If Next currCell 

现在它可以同时使用自动填充或复制/粘贴