【问题标题】:Excel macro hangs some timesExcel 宏有时会挂起
【发布时间】:2017-08-11 18:11:20
【问题描述】:

我在 excel 中编写了一个宏,有时宏会按应有的方式工作,但大约 40% 的时间它会挂起完整的 excel 并且没有任何反应。我试图逐步完成,大多数时候我在 3 个特定语句中找到了宏手。有人可以告诉我我做错了什么,或者如何更好地使宏更健壮和稳定。

以下是宏中的代码:

    Sub fastcloudextractor()
    '
    ' fastcloud extractor Macro
    '
    '    defenitions

        Dim data_arr() As Variant, temp_arr() As Variant
        Dim i As Long, j As Long, k As Long, curent_item As Long
        Dim pctCompl As Integer, err As Integer, total_items As Integer

        Application.ScreenUpdating = False

        err = 2
    '
    '    get data row count and load data into array
    '
        Sheets("Original").Select

        data_count = Range("A1").End(xlDown).Row
        data_count = data_count + 1
        Cells(data_count, 1) = 1
        Cells(data_count, 5) = 1
        data_arr = Range(Cells(2, 5), Cells(data_count, 14))

    '    without Below 2 Lines the program gives a error
    '
        Sheets("sheet4").Select
        temp_arr = Range(Cells(1, 1), Cells(data_count, 10))



    ' ----- Begin new code -----

        k = 1
        current_item = data_arr(1, 1)

    '    Debug.Print current_item

        For j = LBound(data_arr) To UBound(data_arr)

        If data_arr(j, 1) = current_item Then

             do some thing 
        Else
             Do some thing else

        End If

        k = k + 1

    Next j

    Erase temp_arr
    Erase data_arr

    Sheets("Original").Select
    Range("A2:N2").Select
    Sheets("Unique").Select
    Range("A2").Select
    Sheets("Selected").Select
    Range("A1").Select
    Sheets("Compiled").Select
    Range("A2").Select
    Sheets("Extracted").Select
    Range("A1").Select
    Sheets("Magmi").Select

    Application.ScreenUpdating = True
    Application.StatusBar = False
    Beep
    MsgBox "Data Conversion Completed" & vbCr & "Total no of products is .." & total_items

End Sub

宏在

处正常挂起
  1. data_arr = Range(Cells(2, 5), Cells(data_count, 14))

  1. temp_arr = Range(Cells(1, 1), Cells(data_count, 10))

谁能帮我找出我做错了什么以及如何纠正它。

我还是个新手,所以如果提到更正,请给出代码示例。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我发现data_count = Range("A1").End(xlDown).Row 非常可疑。如果只有一行数据,您的 data_count 将等于 1048576,然后使用 data_arr = Range(Cells(2, 5), Cells(data_count, 14)) 您将使用 10,485,760 个值填充此数组。好多啊。最好改用data_count = Range("A" & Rows.Count).End(xlUp).Row

    【讨论】:

    • A1 或第一行有我不会处理的标题。所以基本上数据将来自A2。这不会是空白的
    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多