我正在尝试编写一个代码,如果单元格为空,则可以复制并粘贴之前列中的数据。我的目标是 I、K、M、O、Q 和 S 列。
这是我目前正在使用的:
Sub FillFutureRoles()
Dim iCell As Range
For Each iCell In Range("I:I,K:K,M:M,O:O,Q:Q,S:S")
If iCell.Value = "" Then
iCell.Value = iCell.Offset(0, -1).Value
End If
Next iCell
End Sub
我正在处理一个包含 600 多行且不断增长的数据集,当我尝试运行此代码时,它仍然运行了 30 分钟。我知道该代码可以工作,因为我已经尝试过使用更少的列和更小的数据集样本集,但对于较大的数据集效率不高。
最佳答案
现在无法测试它,但我可能会这样做:
Sub FillFutureRoles()
' get the last row
Dim LastRow As Long, strRange As String
LastRow = ActiveSheet.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
strRange = "H1:S" & CStr(LastRow)
' copy all values from the range to the array
Dim vCells() As Variant, r As Long, c As Long
vCells = Range(strRange).Value
For r = 1 To LastRow
For c = 2 To 12 Step 2
If vCells(r, c) = "" Then
vCells(r, c) = vCells(r, c - 1)
End If
Next c
Next r
' copy all values from the array back to the range
Range(strRange) = vCells
End Sub
关于excel - 如何更快地将数据复制并粘贴到空列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68167579/