Excel中具有400000行的组结构级别会挂起应用程序

pprl5pva  于 2023-10-21  发布在  其他
关注(0)|答案(2)|浏览(81)

我有一个简单的宏,我根据结构级别(1-8)对行进行分组。创建轮廓。当我在一个有76 k行的excel上运行宏时,它需要时间,但完成了任务。但在那之后,如果行增加运行宏挂起Excel,我必须杀死任务管理器的进程。

Sub GroupReport()

    'Define Variables
    Dim StartCell As Range
    Dim StartRow As Long
    Dim LevelCol As Integer
    Dim LastRow As Long
    Dim CurrentLevel As Integer 'iterative counter'
    Dim i As Long
    Dim j As Integer
    
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False 

    Set StartCell = Cells(5, 1)
    StartRow = StartCell.Row
    LevelCol = StartCell.Column
    LastRow = ActiveSheet.UsedRange.Rows.Count

    ' Cells.ClearOutline
    
    'inarr = Range(Cells(5, 1), Cells(LastRow, 19))

    For i = StartRow To LastRow
        CurrentLevel = Cells(i, LevelCol)
        Rows(i).Select
        For j = 1 To CurrentLevel - 1
            On Error Resume Next
            Selection.Rows.Group
        Next j
    Next i

    Application.ScreenUpdating = True 

End Sub

我试着设置手动计算和屏幕更新关闭,以提高性能,但它没有多大帮助。我试图将数据复制到变量数组中,但无法理解如何通过它循环。
我真的很感激任何建议/帮助,使这与4L和提高性能的Excel工作。

yzuktlbb

yzuktlbb1#

我会限制与SELECT方法的交互次数,实际行数而不是UsedRange. Array.Count,并使用数组来加快处理速度。这里是一些关于你的代码的触摸。
SubMy2nds.Optimized()

Dim ws As Worksheet
Dim StartRow As Long, LastRow As Long
Dim LevelCol As Long
Dim CurrentLevel As Integer
Dim i As Long, j As Long
Dim LevelArr As Variant

' Define the working sheet
Set ws = ThisWorkbook.ActiveSheet

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

' Start the timer
Dim StartTime As Double
StartTime = Timer

StartRow = 5
LevelCol = 1
LastRow = ws.Cells(ws.Rows.Count, LevelCol).End(xlUp).Row

' Read data into an array
LevelArr = ws.Range(ws.Cells(StartRow, LevelCol), ws.Cells(LastRow, LevelCol)).Value

' Iterate over rows to group them
For i = 1 To UBound(LevelArr, 1)
    CurrentLevel = LevelArr(i, 1)
    For j = 1 To CurrentLevel - 1
        ws.Rows(StartRow + i - 1).Group
    Next j
Next i

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

' Display runtime
MsgBox "Runtime: " & Round(Timer - StartTime, 2) & " seconds"

End Sub

qvtsj1bj

qvtsj1bj2#

我使用Python中的openpyxl库来复制逻辑,并注意到速度有了显著的提高。对于400,000行的数据集,在A列中随机分配1-8级,测试文件操作仅在79.61秒内完成,没有任何问题。
下面是Python代码。

import openpyxl
from time import time

def group_rows_based_on_levels(filename):
    # Load workbook and select active sheet
    wb = openpyxl.load_workbook(filename)
    ws = wb.active
    
    start_time = time()
    
    start_row = 5
    level_col = 1
    
    # Determine the last row with data in the level column
    last_row = ws.max_row

    for i in range(start_row, last_row + 1):
        current_level = ws.cell(row=i, column=level_col).value
        if current_level is not None:
            for _ in range(current_level - 1):
                ws.row_dimensions[i].outlineLevel += 1
                ws.row_dimensions[i].hidden = True

    # Save the changes
    wb.save(filename)
    
    runtime = round(time() - start_time, 2)
    print(f"Runtime: {runtime} seconds")

# Example usage
group_rows_based_on_levels("your_file.xlsx")

相关问题