作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Excel VBA 中编写了一个宏,它基本上复制粘贴 53 行 1440 次,一个在另一个之下,以便在 ~70000 行表中填充两列。该宏有效,但完全运行大约需要五分钟。如果我不必在大约 1000 个其他文件上运行它,这会很好。我正在寻找任何方法来加快这个过程,这样它就不需要 5 天的时间来运行。
我尝试使用范围复制方法:
Set range1 = {the table I'm copying}
Set range2 = {the cells I want to paste into}
range1.Copy range2
Windows("as_built_comp.xlsm").Activate
Sheets(siteName).Activate
j = Cells(Rows.Count, 1).End(xlUp).Row
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
wb.Activate
Range("I12").Select
For i = 1 To 1440
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=56
ActiveCell.Offset(j - 1, 0).Select
Next i
最佳答案
将其全部加载到一个数组中,然后在最后输出整个数组。重构代码以避免使用激活/选择
Sub tgr()
Dim wbDest As Workbook
Dim wbData As Workbook
Dim wsDest As Worksheet
Dim wsData As Worksheet
Dim aTemp() As Variant
Dim aData() As Variant
Dim SiteName As String
Dim RepeatData As Long
Dim ixTemp As Long
Dim ixData As Long
Dim ixCol As Long
SiteName = "SiteName1"
RepeatData = 1440
Set wbDest = ThisWorkbook
Set wbData = Workbooks("as_built_comp.xlsm")
Set wsDest = wbDest.Worksheets(1)
Set wsData = wbData.Worksheets(SiteName)
With wsData.Range("C2:D" & wsData.Cells(wsData.Rows.Count, "C").End(xlUp).Row)
If .Row < 2 Then Exit Sub 'No data
aTemp = .Value
ReDim aData(1 To .Rows.Count * RepeatData, 1 To .Columns.Count)
End With
For ixData = 1 To UBound(aData, 1)
ixTemp = ((ixData - 1) Mod UBound(aTemp, 1)) + 1
For ixCol = 1 To UBound(aTemp, 2)
aData(ixData, ixCol) = aTemp(ixTemp, ixCol)
Next ixCol
Next ixData
wsDest.Range("I12").Resize(UBound(aData, 1), UBound(aData, 2)).Value = aData
End Sub
关于excel - 如何让 "copy-paste"宏运行得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56548841/
我是一名优秀的程序员,十分优秀!