完善当前代码以减少excel处理时间:删除和还原范围值

时间:2019-03-03 18:30:28

标签: excel vba

我当前的代码可以工作,并且可以将特定范围内的值存储为全局变量,然后用一个按钮从两个Excel工作表中清除此数据,然后用另一个按钮恢复已清除的数据。

问题在于处理时间很长,因为我的工作簿有很多工作表,而且它们都是相互链接的。我想知道是否有人能想到一种重写代码的方式,以便excel更快地处理它?

谢谢!

Option Explicit
Dim arr1 As Variant
Dim arr2 As Variant
Dim arr3 As Variant
Dim arr4 As Variant
Dim arr5 As Variant
Dim arr6 As Variant



Public Sub HidePD()

Dim rngData1 As Range
Dim rngData2 As Range
Dim rngData3 As Range
Dim rngData4 As Range
Dim rngData5 As Range
Dim rngData6 As Range
Dim cell As Range

With ThisWorkbook.Worksheets("Overview_Existing Portfolio EUR")

    arr1 = ""
    arr2 = ""
    arr3 = ""

    Set rngData1 = .Range("E12:I12,K12:DG12")
    Set rngData2 = .Range("E10:I10,K10:DG10")
    Set rngData3 = .Range("E16:I16,K16:DG16")

    arr1 = .Range("E12:DG12")
    arr2 = .Range("E10:DG10")
    arr3 = .Range("E16:DG16")

    rngData1.ClearContents
    rngData2.ClearContents
    rngData3.ClearContents

End With

With ThisWorkbook.Worksheets("Input Sheet LC")

    arr4 = ""
    arr5 = ""
    arr6 = ""

    Set rngData4 = .Range("I76:S103")
    Set rngData5 = .Range("I111:S126")
    Set rngData6 = .Range("I355:S390")

    arr4 = .Range("I76:S103")
    arr5 = .Range("I111:S126")
    arr6 = .Range("I355:S390")

    rngData4.ClearContents
    rngData5.ClearContents
    rngData6.ClearContents

End With

End Sub

Public Sub ShowPD()

With ThisWorkbook.Worksheets("Overview_Existing Portfolio EUR")
        .Range("E12:DG12").Value = arr1
        .Range("E10:DG10").Value = arr2
        .Range("E16:DG16").Value = arr3

End With


With ThisWorkbook.Worksheets("Input Sheet LC")
        .Range("I76:S103").Value = arr4
        .Range("I111:S126").Value = arr5
        .Range("I355:S390").Value = arr6

End With
End Sub

1 个答案:

答案 0 :(得分:0)

他们是对的(上方)-有一些应用程序成员,例如 ScreenUpdating EnableEvents Calculation 。使用Variants会占用最多的内存,在64x上比32x的系统上更多,并且看起来您正在使用6,并且它们是动态长度-占用最多的内存。如果您有调用其他过程的过程,这些变量将一直存在,直到全部完成为止。所以也许在每个子功能的末尾清理变量。

(对不起,不能添加评论,因为我需要更多信誉点!:))