因此,我有一個宏,用於在工作表的打印區域中爲每個分頁符插入4個標題行。當我在調試模式下逐步運行它時,它會爲每個分頁符插入正確的標題行,但是當它本身運行時,它似乎在跳過部件。我已經添加了Sleeps和Debug.Prints,以便找出它出錯的地方,我仍然無法弄清楚。Excel宏在調試中工作,但不能全面運行
下面是代碼:
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
任何建議或幫助將不勝感激。
感謝, 凱文
我花了幾個小時試圖弄清楚如何完成這一切,所花的一切都是那麼一點點一段代碼。哇。謝謝!!!這不是我曾經使用過的一段代碼。每天學些新東西。再次感謝。 「DoEvenets」爲 – Peage1475
+ 1 –