excel - 用 "With"和 "Do While"替换 Select 语句后,VBA 运行速度非常慢

标签 excel vba loops with-statement

所以,我有一些 VBA 代码,它基本上将一些新列插入数据集,在每个新列中放置一个公式并将其拖到数据集的末尾。
在我进行更改之前,代码看起来像这样。

Range("A1").Select
ActiveCell.FormulaR1C1 = "Size"
Range("A2").Select
    ActiveCell.FormulaR1C1 = _
        "=('Data'!RC[3])+IF('Data'!RC[1]>0,'Data'!RC[1],""0"")"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)

Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "client"
Range("A2").Select
ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[46],'[Client codes.xlsx]Sheet1'!C1:C2,2,0),""No"")"
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
在阅读了几篇关于如何使用 Select 等使您的代码更加错误的文章之后,我想我会使用“with”语句和“do while”循环来更新我的代码。但是,我发现的一件事是,现在我的代码运行时间可能要长 10 倍。如在它运行大约 2 分钟的所有列添加和公式集之前,现在我不得不在运行大约 15 分钟后中断代码两次。
我更新的代码如下:
Set r = Range("A:A")

With r
 .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End With

Set title = Range("A1")
Set cell = Range("A2")

With title
.FormulaR1C1 = "Size"
End With

With cell
Do While cell <= LastRow
    .FormulaR1C1 = "=('Data'!RC[3])+IF('Data'!RC[1]>0,'Data'!RC[1],""0"")"
    .AutoFill Destination:=Range("A2:A" & LastRow)
    .Offset(1, 0).Select
Loop
End With

Set r = Nothing
Set title = Nothing
Set cell = Nothing

Set r = Range("A:A")

With r
 .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End With

Set title = Range("A1")
Set cell = Range("A2")

With title
.FormulaR1C1 = "client"
End With

With cell
Do While cell <= LastRow
    .FormulaR1C1 =  "=IFERROR(VLOOKUP(RC[46],'[Client codes.xlsx]Sheet1'!C1:C2,2,0),""No"")"
    .AutoFill Destination:=Range("A2:A" & LastRow)
    .Offset(1, 0).Select
Loop
End With

Set r = Nothing
Set title = Nothing
Set cell = Nothing
我考虑过删除自动填充,因为我有一个 Do While 循环,但它只填充每列的第二行。
任何想法我做错了什么?

最佳答案

您可以显着减少代码的大小:

Sub test1()
    Dim colnames, formulae, i
    
    colnames = Array("Size", "Client")
    formulae = Array("=('Data'!RC[3])+IF('Data'!RC[1]>0,'Data'!RC[1],""0"")", _
               "=IFERROR(VLOOKUP(RC[46],'[Client codes.xlsx]Sheet1'!C1:C2,2,0),""No"")")
    
    With ActiveSheet 'it is better to specify the exact sheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 0 To UBound(colnames)
            .Columns("A").Insert CopyOrigin:=xlFormatFromRightOrBelow
            .Range("A1") = colnames(i)
            .Range("A2").Resize(LastRow - 1).FormulaR1C1 = formulae(i)
        Next
    End With
End Sub

关于excel - 用 "With"和 "Do While"替换 Select 语句后,VBA 运行速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68755914/

相关文章:

excel - 有没有办法查看一个单元格是否在公式中使用并用条件格式标记它

vba - 如果不同工作表上的值匹配,则将数据从第一工作表复制到其他工作表

excel - 使用 if 语句时移动到下一个单元格

vba - Excel VBA - PivotItems 返回无效值

php while循环不会增加数据库列

VBA检索超链接目标表?

vba - 1004 错误 - 将公式动态分配给单元格范围

excel - 如何在vba中的字符串中添加双引号?

python - 赋值 : Prompt user to input their Birthmonth then input the day they were born. 打印月份名称,后跟日期

c++ - 为什么这个 C++ 循环不是从 10 计数到 -5?