Thread: Error Handling
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Steph[_6_] Steph[_6_] is offline
external usenet poster
 
Posts: 92
Default Error Handling

Hello. Thanks to the help of many of you, I have the procedure below. Can
someone help with error handling? The code prints out sheets within a
file - both of which are determined from cell values within a spreadsheet.
I would like to capture the error if excel opens a book and tries to print a
sheet that does not exist. Thanks in advance!

datapath = ThisWorkbook.ActiveSheet.Range("A3").Value
Set pr = ThisWorkbook.ActiveSheet.Cells(ActiveCell.Row, 3)
If Trim(pr.Value) < "" Then
WbOpen = datapath & pr.Value
Set bk = Workbooks.Open(WbOpen)
Set rng1 = pr.Offset(0, 1).Resize(1, 50).SpecialCells(xlConstants)
For Each cell1 In rng1
If LCase(cell1.Value) = "all" Then
bk.PrintOut
Cells(pr.Row, 3).Interior.ColorIndex = 35
Exit For
Else
If Trim(cell1.Value) < "" Then
bk.Worksheets(cell1.Value).PrintOut
End If
End If
Next
Cells(pr.Row, 3).Interior.ColorIndex = 35
End If
WbOpen = ""
bk.Close SaveChanges:=False