algorithm - 帮助我优化 VBA Excel 代码,用于将工作表上每一行的某些列复制到另一张

标签 algorithm excel optimization vba ms-office

我需要将工作表 A 中每一行的某些列复制到工作表 B 中。

我创建了一个 sub,它创建了匹配列号的 2 个数组(变体),因此我可以将工作表 A 中的第 3 列映射为等于工作表 B 中的第 8 列,等等。

一切正常,问题是它相当慢,这里是:

Sub insertIntoSelectedOpps(opCols As Variant, siebelCols As Variant, ByVal length As Integer)

Dim insertRange As Range
Dim siebelRange As Range
Dim rowCount As Integer

Set insertRange = shSelected.Range("a3", "bb4") 'very wide table!'
Set siebelRange = shDatabase.UsedRange.Rows

rowCount = siebelRange.Rows.count

MsgBox "siebel row count: " & rowCount

For i = 2 To rowCount

    Set insertRange = shSelected.Range("a3", "bb4")
    insertRange.Insert

    For x = 1 To length - 1
        If opCols(x) <> -1 Then 'unequal to -1'
            insertRange.Cells(1, opCols(x)).value = siebelRange.Cells(i, siebelCols(x)).value
        End If
    Next x

Next i
End Sub

不要担心 IF 情况,这是业务逻辑,以防找不到映射中的列名。

最佳答案

通常,在大处理之前停止屏幕更新和计算是个好主意:

msgbox 之后:

Application.ScreenUpdating = False
xlCalc = Application.Calculation
Application.Calculation = xlCalculationManual

next之后

Application.Calculation = xlCalc
Application.ScreenUpdating = True

这应该会加快速度。另外,看看 this page进行更多速度调整

关于algorithm - 帮助我优化 VBA Excel 代码,用于将工作表上每一行的某些列复制到另一张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3332380/

相关文章:

c++ - 当人们想在函数中操作指针时,为什么要传入一个双指针呢?

mysql - 如何优化 MySQL View

algorithm - 座位有多少种排列方式

algorithm - Geohashes - 为什么需要交错索引值?

VBA 将范围另存为工作簿

c# - 如何在 C# 中将 Excel 值转换为日期时间格式

excel - 我的 VBA 方法导致 Excel 崩溃 - 我看不到错误

performance - 如何在 Go 中通过清晰的结构值优化性能?

algorithm - 带边成本的 Dijkstra 最短路径算法

java - 将范围列表缩小为整数数组