Excel VBA - 自动填充仅在调试期间有效

时间:2014-08-11 14:21:39

标签: excel vba autofill

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

我的宏基本上从工作表“需求”中的排序表中获取所有可见单元格,并将它们的值直接复制到工作表“ITA& IO-EOTP”中的表格下。该表自动扩展,但并非所有公式都应用于新行(但有些公式)。然后我从上面的行调用自动填充到新行。复制完所有新ID后,我会对所有内容进行排序。

事情是:当我逐行调试时自动填充工作,当我启动没有断点的宏时,它会被简单地跳过...我不知道这是什么原因!我已经花了一整天的时间试图解决它,这让我疯了!

这是整个宏:

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表有一个宏,只要它被激活就会将.EnableCalculation设置为False,不知道它是否重要/帮助)

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

1 个答案:

答案 0 :(得分:1)

好的,找到了罪魁祸首......我只需要在编译器进入For block之前激活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

...现在它适用于自动填充或复制/粘贴