VBA: Workbooks.Open Error handling within a For Next loop

hb_123

New Member
Joined
Apr 4, 2012
Messages
11
Hi, I'm fairly new to vba so any help is appreciated!

I have a macro that loops through 5 cells B20-B24, opens the files specified in cells, copies the data and stacks them in the main workbook.
What I would like to include is if one on of the files in B20-B24 doesn't exist, for the code to go to the next iteration.

I've tried including a On Error Resume Next before and after the workbooks.open bit but that doesn't seem to work

Code below:
Code:
Sub Copy_Cash()

Application.ScreenUpdating = False
Dim template As Workbook
Dim macro As Worksheet

Set template = ActiveWorkbook
Set macro = template.ActiveSheet

For i = 20 To 24
Workbooks.Open macro.Range("B" & i).Value
Range("A2:P2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("Template.xlsb").Worksheets("Cash Files").Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
ActiveWorkbook.Close SaveChanges:=False
Next i

End Sub
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You can test the existence of the file using Dir:

Code:
Sub Copy_Cash()

Application.ScreenUpdating = False
Dim template As Workbook
Dim macro As Worksheet
Dim wb as workbook
Set template = ActiveWorkbook
Set macro = template.ActiveSheet

For i = 20 To 24
If Dir(macro.Range("B" & i).Value) <> vbnullstring then 
Set wb = Workbooks.Open(macro.Range("B" & i).Value)
With wb.Activesheet
.Range("A2:P2", .Range("A2:P2").End(xlDown)).Copy
End With
template.Worksheets("Cash Files").Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
wb.Close SaveChanges:=False
end if
Next i

End Sub
 
Upvote 0
Worked perfectly! Could this be extended to check if worksheets (named in the main workbook in cells A1-A5) exist ?
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

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