我的代码运行速度很慢,是2 For循环导致吗?
谢谢
For x = LBound(dataArray) To UBound(dataArray) 'define start and end of array, lower bound to upper bound
For Each rngcell In Range("A:B") 'lookup each cell in row 1
If dataArray(x) = rngcell.Value Then ' if cells in header row match with values in array
rngcell.EntireColumn.Copy ' then copy whole column of data for that parameter
Sheets(3).Select ' select sheet to paste data
Range("A:B").End(xlUp).Offset(rowOffset:=0, columnOffset:=x).Select 'select area to paste, paste in next column - no. x
Selection.PasteSpecial xlPasteValues ' paste
End If
Next rngcell ' next header row cell
Next x
End Sub
答案 0 :(得分:1)
只是一些建议:
.Select
会导致Excel更新UI,这很昂贵。尝试计算目标单元格/范围ONCE并使用它来调用PasteSpecial而不是Selection。答案 1 :(得分:1)
您正在使用Range(A:B)
,这肯定会降低您的代码速度。
Excel将根据您的代码基本读取该范围内的每个单元格。
那200万个细胞。
尝试使用类似Replace(Range("B").End(Xldown).Address,"$B$","")
的内容来限制B的范围。