Page Break Macro

  • Thread starter Thread starter Crowbar
  • Start date Start date
C

Crowbar

Hi

I have the macro that searches for text information and then inserts a page
break when it finds it.

However, if the text ins't in the spread sheet then it crashes, below is my
code

Cells.Find(What:="CM", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:
= _
True, SearchFormat:=False).Activate
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell

Does anyone know what could be put into this to ignore the page break command
if the txt isn't found in the sheet??

Regards Gaz
 
dim FoundCell as range

set foundcell = Cells.Find(What:="CM", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)

if founcell is nothing then
'do nothing
else
if foundcell.row > 1 then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=foundcell
end if
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top