Error handling

kknb4591

New Member
Joined
Aug 25, 2011
Messages
10
Hi,

Have searche high and low for an answer to this one, I think it should be pretty simple.

I have a piece of code which opens two files one-by-one and reformats them before closing them:

---First bit
Workbooks.Open Filename:= _
Range("D6")
Rows("1:18").Select
Range("A18").Activate
Selection.Delete Shift:=xlUp
Cells.Select
Cells.EntireRow.AutoFit
Selection.RowHeight = 12
Cells.EntireColumn.AutoFit
Range("A1").Select
ActiveWorkbook.SaveAs Filename:= _
"H:\file1.xls", _
FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close

---Second bit
Workbooks.Open Filename:= _
Range("D11")
Cells.Select
Selection.RowHeight = 12
Range("A1").Select
Cells.Find(What:="Global Treasury Services", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(-4).Resize(5).ClearContents
Range("A1").Select
ChDir "H:\"
ActiveWorkbook.SaveAs Filename:= _
"H:\file2.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub

But what I need to do is if there is no file for the "first bit" to reformat then to just skip on to the "second bit" and complete that.

I'd be grateful for any help with this.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Not ideal but

On Error Goto SecondBit
Workbooks.Open Filename:= _
Range("D6")
Rows("1:18").Select
Range("A18").Activate
Selection.Delete Shift:=xlUp
Cells.Select
Cells.EntireRow.AutoFit
Selection.RowHeight = 12
Cells.EntireColumn.AutoFit
Range("A1").Select
ActiveWorkbook.SaveAs Filename:= _
"H:\file1.xls", _
FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close

Secondbit:
Workbooks.Open Filename:= _
Range("D11")
Cells.Select
Selection.RowHeight = 12
Range("A1").Select
Cells.Find(What:="Global Treasury Services", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(-4).Resize(5).ClearContents
Range("A1").Select
ChDir "H:\"
ActiveWorkbook.SaveAs Filename:= _
"H:\file2.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,711
Members
452,939
Latest member
WCrawford

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