Excel VBA macro to drag off the page break preview right side

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
204
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
End Sub

I have recorded a macro to drag off the direction of page break to right, I have 30+ sheets and I want that the code to run over all sheets except sheet name "Count" & "Raw Data". What should be the code ??
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The following macro will loop through each worksheet in the active workbook, and for each worksheet which is not named "Count" or "Raw Data", it removes the first vertical page break. Note that it simply uses the Delete method instead.

VBA Code:
Sub DeleteFirstVerticlePageBreakInWorksheets()

    Dim currentWorksheet As Worksheet
    
    On Error Resume Next
    For Each currentWorksheet In ActiveWorkbook.Worksheets
        If currentWorksheet.Name <> "Count" And currentWorksheet.Name <> "Raw Data" Then
            currentWorksheet.VPageBreaks(1).Delete
        End If
    Next currentWorksheet
    On Error GoTo 0
    
End Sub

Hope this helps!
 
Upvote 0
I tried the code

VBA Code:
Sub DeleteFirstVerticlePageBreakInWorksheets()

    Dim currentWorksheet As Worksheet
    
    On Error Resume Next
    For Each currentWorksheet In ActiveWorkbook.Worksheets
        If currentWorksheet.Name <> "COUNT" And currentWorksheet.Name <> "RAW DATA" Then
            currentWorksheet.VPageBreaks(1).Delete
        End If
    Next currentWorksheet
    On Error GoTo 0
    
End Sub


test1.PNG
test1-1.PNG



But it didnt delete the page break not dragged towards right.
 
Upvote 0
Oh I see, try the following instead...

VBA Code:
Sub DeleteFirstVerticlePageBreakInWorksheets()

    Dim currentWorksheet As Worksheet
    
    For Each currentWorksheet In ActiveWorkbook.Worksheets
        If currentWorksheet.Name <> "COUNT" And currentWorksheet.Name <> "RAW DATA" Then
            With currentWorksheet
                If .VPageBreaks.Count > 0 Then
                    .VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
                End If
            End With
        End If
    Next currentWorksheet
    
End Sub

Does this help?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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