excel - 我需要使用 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
解决方案
拖累:
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
推荐阅读
- git - 在 Git Bash 中,命令 `npm start` 返回错误
- arrays - 带有 GSOn 的 JSONArray
- c++ - 从字符串中解析复数,考虑语法变化
- android - Android Studio IDE 错误错误
- openpyxl - 尝试使用 openpyxl 遍历 2 列以查找特定行
- javascript - 如何使第二个列表项被删除一次,而不是两次?
- php - Android JSON POST 方法
- python - AttributeError:模块'inspect'没有使用Textmate的属性'getsource'
- spring-security-oauth2 - Azure AD B2C 是否为每个注册或登录策略提供一个端点,例如来自 OIDC 的 user-info-uri?
- python - 尝试使用 read_csv w/to_sql 将行附加到 SQL 表时,由于列中的额外逗号导致 Pandas 解析错误