ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Page break code - hpagebreak before value (https://www.excelbanter.com/excel-programming/434899-page-break-code-hpagebreak-before-value.html)

Maarkr

Page break code - hpagebreak before value
 
Need to have a page break before every instance of *dept* in a column... this
doesn't work. Do multiple columns mess it up?

Cells.PageBreak = xlPageBreakNone
col = 1
LastRw = 3300
For x = 1 To LastRw
If Cells(x, col).Value = "*dept*" Then 'or like "*dept*"
ActiveWindow.SelectedSheets.HPageBreaks.Add Befo=Rows(x)
End If
Next
ActiveWindow.View = xlPageBreakPreview
'ActiveSheet.ResetAllPageBreaks
End Sub

Jacob Skaria

Page break code - hpagebreak before value
 
Try the below....Have you tested the other macro...with the sample posted...

Sub MyMacro()
Dim lngRow As Long
For lngRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1
If lngRow < 1 Then
If UCase(Right(Range("A" & lngRow), 5)) = " DEPT" Then
ActiveSheet.HPageBreaks.Add Befo=Range("A" & lngRow)
End If
End If
Next
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Maarkr" wrote:

Need to have a page break before every instance of *dept* in a column... this
doesn't work. Do multiple columns mess it up?

Cells.PageBreak = xlPageBreakNone
col = 1
LastRw = 3300
For x = 1 To LastRw
If Cells(x, col).Value = "*dept*" Then 'or like "*dept*"
ActiveWindow.SelectedSheets.HPageBreaks.Add Befo=Rows(x)
End If
Next
ActiveWindow.View = xlPageBreakPreview
'ActiveSheet.ResetAllPageBreaks
End Sub


Maarkr

Page break code - hpagebreak before value
 
it won't work because the text 'dept' is in a different place thruout the
cell... it may be =678899 UFFH Finance Dept ****** or =**884 Marktg Dept
Anytown MI
so the Right(Range("A" & lngRow), 5)) = " DEPT" won't work... can't I use a
wildcard???

"Jacob Skaria" wrote:

Try the below....Have you tested the other macro...with the sample posted...

Sub MyMacro()
Dim lngRow As Long
For lngRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1
If lngRow < 1 Then
If UCase(Right(Range("A" & lngRow), 5)) = " DEPT" Then
ActiveSheet.HPageBreaks.Add Befo=Range("A" & lngRow)
End If
End If
Next
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Maarkr" wrote:

Need to have a page break before every instance of *dept* in a column... this
doesn't work. Do multiple columns mess it up?

Cells.PageBreak = xlPageBreakNone
col = 1
LastRw = 3300
For x = 1 To LastRw
If Cells(x, col).Value = "*dept*" Then 'or like "*dept*"
ActiveWindow.SelectedSheets.HPageBreaks.Add Befo=Rows(x)
End If
Next
ActiveWindow.View = xlPageBreakPreview
'ActiveSheet.ResetAllPageBreaks
End Sub


Jacob Skaria

Page break code - hpagebreak before value
 
OK. Try the below

Sub MyMacro()
Dim lngRow As Long
For lngRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1
If lngRow < 1 Then
If Instr(1, Range("A" & lngRow), " DEPT", vbTextCompare) 0 Then
ActiveSheet.HPageBreaks.Add Befo=Range("A" & lngRow)
End If
End If
Next
End Sub


If this post helps click Yes
---------------
Jacob Skaria


"Maarkr" wrote:

it won't work because the text 'dept' is in a different place thruout the
cell... it may be =678899 UFFH Finance Dept ****** or =**884 Marktg Dept
Anytown MI
so the Right(Range("A" & lngRow), 5)) = " DEPT" won't work... can't I use a
wildcard???

"Jacob Skaria" wrote:

Try the below....Have you tested the other macro...with the sample posted...

Sub MyMacro()
Dim lngRow As Long
For lngRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row + 1
If lngRow < 1 Then
If UCase(Right(Range("A" & lngRow), 5)) = " DEPT" Then
ActiveSheet.HPageBreaks.Add Befo=Range("A" & lngRow)
End If
End If
Next
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Maarkr" wrote:

Need to have a page break before every instance of *dept* in a column... this
doesn't work. Do multiple columns mess it up?

Cells.PageBreak = xlPageBreakNone
col = 1
LastRw = 3300
For x = 1 To LastRw
If Cells(x, col).Value = "*dept*" Then 'or like "*dept*"
ActiveWindow.SelectedSheets.HPageBreaks.Add Befo=Rows(x)
End If
Next
ActiveWindow.View = xlPageBreakPreview
'ActiveSheet.ResetAllPageBreaks
End Sub



All times are GMT +1. The time now is 12:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com