Works with F8, not when running

dchaney

Well-known Member
Joined
Jun 4, 2008
Messages
732
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am trying to figure out why my code will work for each iteration of an HPageBreak when I use F8, but when I run it fully it only works on the first iteration, but then it skips the rest. Is there something I am missing within my code, and if so can someone help me with it? here is my code:

Code:
'******************************************************************************************************************************
'Move Page Breaks above current selected cell
'******************************************************************************************************************************
Dim i As Integer, pb As Integer, pbRow As Integer

i = 1
pb = ActiveSheet.HPageBreaks.Count
Do
    If pb = 0 Then
        Exit Do
    End If
    
    pbRow = ActiveSheet.HPageBreaks(i).Location.Row
    
    If Not IsEmpty(Range("B" & pbRow).Value) Then
    'Cell is not empty therefore find the first occurrence of blank in Col B above this row
        Do
            'Loopback until there is an empty cell in Col B
            pbRow = pbRow - 1
        Loop Until IsEmpty(Range("B" & pbRow))
            'Set the new Page break above the empty cell
            Range("B" & pbRow + 1).Select
            ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
            
    End If
    
    i = i + 1

Loop Until i > pb

Any help will be appreciated, thanks in advance
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hello All,

I figured this out, so wanted to post my results in case others run into this same issue. I made the following adjustments to the code. The change that allowed for me to get this to work was adding ActiveWindow.View = xlPageBreakPreview, everything else was clean up trying to figure it all out.

Code:
'******************************************************************************************************************************
'Move Page Breaks above current Selected Cells
'******************************************************************************************************************************
ActiveWindow.View = xlPageBreakPreview

i = 1
pb = ActiveSheet.HPageBreaks.Count

Do Until i > pb
    
    pbRow = ActiveSheet.HPageBreaks(i).Location.Row
    
    If Not IsEmpty(Range("B" & pbRow).Value) Then     'Cell is not empty therefore find the first blank cell in Col B above this row
    
        Do Until IsEmpty(Range("B" & pbRow))          'Loopback until there is an empty cell in Col B
            pbRow = pbRow - 1
        Loop
            
        Range("B" & pbRow + 1).Select                                                         'Select the cell below the empty cell
        ActiveSheet.SelectedSheets.HPageBreaks.Add Before:=ActiveCell            'Set the new Page break above the selected cell
            
    End If
    
    i = i + 1

Loop

ActiveWindow.View = xlNormalView
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top