2

所以我有一个宏,旨在为工作表的打印区域中的每个分页符插入 4 个标题行。当我在调试模式下逐步运行它时,它将为每个分页符插入正确的标题行,但是当它自行运行时,它似乎正在跳过部分。我添加了 Sleeps 和 Debug.Prints 以找出问题所在,但我仍然无法弄清楚。

这是代码:

Sub InsertRowPageBreak()

    Dim WS As Worksheet
    Dim rng As Range
    Dim pb As Variant
    Dim Row As Integer
    Dim OffSet As Integer
    Dim InsertRow As Integer

    Set WS = ThisWorkbook.Worksheets(1)
    WS.Activate
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Dim i As Integer
    i = 1

    For Each pb In WS.HPageBreaks
        Debug.Print "Iteration: " & i
        i = i + 1

        Row = pb.Location.Row
        Range("A" & Row).Select
        Debug.Print "Page Break at Row: " & Row

        If (Range("A" & Row - 2).Value Like "*Date*") Then
            InsertRow = Row - 4
            Range("A" & InsertRow).Select
            ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
            Debug.Print "Inserting Page Break @ Row: " & InsertRow
        Else
            Sleep 150
            InsertRow = Row - 1
            Debug.Print "Inserting Row " & InsertRow
            If (Range("D" & InsertRow).Value Like "*Compliment*") Then
                Sleep 150
                Sheets(2).Activate
                Rows("1:4").Select
                Selection.Copy
                Sheets(1).Activate
                Range("A" & InsertRow).Select
                Selection.Insert Shift:=xlDown
                ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
                Debug.Print "Inserted Header 1"
            ElseIf (Range("D" & InsertRow).Value Like "*Complaint*") Then
                Sleep 150
                Sheets(2).Activate
                Rows("5:8").Select
                Selection.Copy
                Sheets(1).Activate
                Range("A" & InsertRow).Select
                Selection.Insert Shift:=xlDown
                ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
                Debug.Print "Inserted Header 2"
            ElseIf (Range("D" & InsertRow).Value Like "*Question*") Then
                Sleep 150
                Sheets(2).Activate
                Rows("9:12").Select
                Selection.Copy
                Sheets(1).Activate
                Range("A" & InsertRow).Select
                Selection.Insert Shift:=xlDown
                ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
                Debug.Print "Inserted Header 3"
            End If
            Sleep 250
        End If
        Sleep 250
    Next pb

End Sub 

当我在调试模式下运行它时,Debug.Print 打印出来

Iteration: 1
Page Break at Row: 33
Inserting Row 32
Inserted Header 1

Iteration: 2
Page Break at Row: 66
Inserting Row 65
Inserted Header 1

Iteration: 3
Page Break at Row: 94
Inserting Row 93
Inserted Header 2

Iteration: 4
Page Break at Row: 119
Inserting Row 118
Inserted Header 3

当它自己运行时

Iteration: 1
Page Break at Row: 33
Inserting Row 32
Inserted Header 1

Iteration: 2
Page Break at Row: 35
Inserting Row 34

Iteration: 3
Page Break at Row: 92
Inserting Row 91
Inserted Header 2

Iteration: 4
Page Break at Row: 94
Inserting Row 93

任何建议或帮助将不胜感激。

谢谢,凯文

4

2 回答 2

5

插入PagebreakExcel 后需要重新分页才能更新HPageBreaks集合。

为了允许 Excel 在代码运行时执行此操作,请DoEvents使用Sleep

于 2013-01-13T03:22:45.483 回答
-1

我目前无法访问 Excel,因此无法准确回答您的问题,但如果我是您,我会做一些不同的事情,因为现在您的代码看起来很容易出错。

首先,每当您通过循环向工作表添加或删除行时,都应该让它倒退。例如:

dim i as integer

for i = mySheet.usedrange.rows.count to 1 step -1

  'Put your code here

next i

在这种情况下,您必须先循环遍历分页符并识别行号,然后以相反的顺序遍历行号,但这可能会有所帮助。

其次,将您将使用的任何工作表分配给一个变量,并引用它们而不是调用激活方法。我以前也用过activate,但后来知道这是一个菜鸟错误,老实说,我不记得为什么我首先使用它。例如,不要这样做:

Sheets(2).Activate
Rows("1:4").Select
Selection.Copy
Sheets(1).Activate
Range("A" & InsertRow).Select

做这样的事情:

dim myBook as workbook, a as worksheet, b as worksheet

set myBook = Excel.ActiveWorkbook
set a = myBook.Sheets(1)
set b = myBook.Sheets(2)

b.rows("1:4").Select
Selection.Copy
a.Range("A" & InsertRow).Select

就像我说的那样,我知道这并不能完全回答您的问题,如果不是很长,我会将其放在评论中,但我真的认为这将大大减少将来的错误并有助于您的整体代码闻。如果我现在有 Excel,我可以做得更多。对不起,我不能提供更多帮助,但祝你好运!

于 2013-01-13T03:01:49.630 回答