Ошибка выполнения 1004: сгруппировать список Excel по уровню отступа с помощью VBA - PullRequest
0 голосов
/ 08 июля 2019

В настоящее время я пытаюсь исправить макрос Excel VBA. Конечная цель - сгруппировать список по уровню отступа.

Структура похожа на это:

Строка 1 .... Строка2 .. Строка3

Einheit ----- A -40

Einheit ------ B -20

Einheit ------ C -20

Einheit ------ D 0

Странно то, что макрос работает до строки 409. В строке 410 я получаю ошибку времени выполнения 1004. Уровень отступа в строке 410 равен 9. Возможно, у вас, ребята, есть идея.

+++ Хорошо, я обнаружил, что ошибка времени выполнения каким-то образом связана с уровнем отступа. Он всегда появляется после строки с уровнем отступа 9. +++

Sub AutoGroupBOM()
    'Define Variables
    Dim StartCell As Range 'This defines the highest level of assembly, usually 1, and must be the top leftmost cell of concern for outlining, its our starting point for grouping'
    Dim StartRow As Integer 'This defines the starting row to beging grouping, based on the row we define from StartCell'
    Dim LevelCol As Integer 'This is the column that defines the assembly level we're basing our grouping on'
    Dim LastRow As Integer 'This is the last row in the sheet that contains information we're grouping'
    Dim CurrentLevel As Integer 'iterative counter'
    Dim i As Integer
    Dim j As Integer

    Application.ScreenUpdating = False 'Turns off screen updating while running.

    'Prompts user to select the starting row. It MUST be the highest level of assembly and also the top left cell of the range you want to group/outline"
    Set StartCell = Application.InputBox("Select top left cell for highest assembly level", Type:=8)
    StartRow = StartCell.Row
    LevelCol = StartCell.Column
    LastRow = ActiveSheet.UsedRange.Rows.Count

    'Remove any pre-existing outlining on worksheet, or you're gonna have 99 problems and an outline ain't 1
    Cells.ClearOutline

    'Walk down the bom lines and group items until you reach the end of populated cells in the assembly level column
    For i = StartRow To LastRow

        Rows(i).Select
        Level = Cells(i, LevelCol).IndentLevel
        For j = 1 To Level - 1
            Selection.Rows.Group
        Next j
    Next i

    Application.ScreenUpdating = True 'Turns on screen updating when done.

End Sub

Заранее спасибо!

1 Ответ

1 голос
/ 22 июля 2019

Эй, я выяснил, в чем проблема.

Макс.Уровень группировки в Excel равен 8, поэтому, как только я попытался сгруппировать в девятый раз (уровень отступа 9), я получил ошибку времени выполнения!

...