首页 > 解决方案 > 我需要使用 VBA 拖动数组公式并粘贴值,但 excel 不断崩溃

问题描述

我正在尝试为一系列单元格下拉数组公式,然后复制/粘贴相应的值。但是,我的excel一直崩溃。基本上,公式转到特定行,然后所有值都被硬粘贴到工作表中。我是 VBA 新手,所以请随时剖析我的代码并提出建议。

Dim lastRow1 As Long
lastRow1 = 37

'change i to whatever your starting row is. Cells(i, #), the # indicates the column index
For i = 3 To lastRow1
Cells(i, 10).Value = Cells(i, 6)
Cells(i, 6).FormulaArray = ""array formula"
Cells(i, 6).Copy
Cells(i, 6).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

标签: excelvba

解决方案


拖累:

   Range("A6").Select
   ActiveCell.FormulaR1C1 = "formula"
   Selection.AutoFill Destination:=Range("A6:A" & lastRow1)

复制粘贴值:

   Range("A6:A" & lastRow1).Select
   Selection.Copy
   Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

lastRow 函数(lastRow1 = fLastRow而不是 lastRow1 = 37):

Function fLastRow() As Long
    fLastRow = 0
    If WorksheetFunction.CountA(Cells) > 0 Then
        fLastRow = Cells.Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious).Row
    End If
End Function

推荐阅读