test if object var is set to nothing also looping through excel sh

  • Thread starter Thread starter SAm
  • Start date Start date
S

SAm

hi

i have two questions. first is regarding my errors. i am trying to close the
excel spreadsheet if sub errs. I am not sure how to test the variable
MyWorkbook or MyExcelSpreadsheet.

Exit_GenerateInvoice:
Exit Sub

Err_GenerateInvoice:
MsgBox Err.Description
If (here is my question) MyWorkbook Then
MyWorkbook.Close SaveChanges:=False
MyExcelInstance.Quit
End If
Resume Exit_GenerateInvoice
End Sub

second question, (unrelated, this is more of an excel question) how to i
loop through sheets of a spreadsheet. this is my script, but it gives me
errors.

For Each sSheet In MyWorkbook
Debug.Print sSheet.Name
Next sSheet
 
Back
Top