Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50110

Macro skipping If statements when run but works in debug

$
0
0
I have a sub that when I run it in debug mode and run it step by step it runs perfectly, however when you let it run by itself it skips half of the if statements. Here is the code:

Code:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

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
   
  'switch to page break view to capture page breaks properly
  ActiveWindow.View = xlPageBreakPreview
    Sheets(1).Activate
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
   
    Set WS = ThisWorkbook.Worksheets(1)
    WS.Activate
    For Each pb In WS.HPageBreaks
       
        Row = pb.Location.Row
       
        Range("A" & Row).Select
        If (Range("A" & Row - 1) = "Date") Then
            'Add page break above the header that is already there and skips adding a new header
            InsertRow = Row - 4
            Range("A" & InsertRow).Select
            ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
        Else
            'Added sleep to see if that helped
            Sleep 1500
            'copy and insert headers depending on section then add pagebreak above header
            InsertRow = Row
            If (Range("V" & InsertRow) = "Positive") Then
                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 "1"
            ElseIf (Range("V" & InsertRow) = "Negative") Then
                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 "2"
            ElseIf (Range("V" & InsertRow) = "Question") Then
                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 "3"
            End If
        End If
    Next pb
 
End Sub

When I run this step by step the debug.print printed 1113 (inserted 4 “header rows” to the top of the pages) but when I run it only prints 13 (inserted 2 “header rows”). So it looks like it got the first one for Positive but not the next two. At first I thought it had to do with the data that is in the cell that it is looking at, but given the same data run and debug return different results. I have tried to add sleep time to this to see if the link was the time I was added when debugging but that did not work. Any insight or suggestions would be greatly appreciated.

Viewing all articles
Browse latest Browse all 50110