VBA to open next file if current file not found

gururaj1986

New Member
Joined
Mar 14, 2017
Messages
11
Hi All,
I am a beginner in VBA & currently working on a project where I need to open a few files files & paste the data from there to one master file (respective sheets) so that a template runs in the master file picking up values from each of these sheets. However there are certain cases where all 5 files are not required. So I wanted to know how can I add a code that can skip to the next file if there is an error in opening file. Below is an example of how the code looks:

Rich (BB code):
    ThisWorkbook.Activate
    Strfile = Sheet1.Range("M3").Value
    'need to add a code here that can skip to first line of next code i.e thisworkbook.activate'

    Workbooks.Open Filename:=Strfile
    Range("A1:M150").Copy
    wbaudit.Activate
    Sheets("IMO EMV Holding").Activate
    Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Application.CutCopyMode = False
    Workbooks.Open Filename:=Strfile
    ActiveWorkbook.Close
    
    ThisWorkbook.Activate
    Strfile = Sheet1.Range("M4").Value
    Workbooks.Open Filename:=Strfile
    Range("A1:S90").Copy
    wbaudit.Activate
    Sheets("Perf & Risk Summary").Activate
    Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Application.CutCopyMode = False
    Workbooks.Open Filename:=Strfile
    ActiveWorkbook.Close

Please do help out :)
Thanks, Guru
 
Hey Sayth,

Thanks for sharing the link. I was not able to figure out how to use if, else & then statements in this situation. Can you please share an example on how to use that in the above code?

Cheers
 
Upvote 0
you can use Power Query tool for your purpose. I used to extract similar data using VBA but power query made is very simple. you can download this tool from internet and install for your excel as add-in.
 
Upvote 0
Something like this untested.

Code:
Dim strFile As String
Dim WB As Workbook
strFile = Sheet1.Range("M3").Value
Dim DirFile As String
If Len(strFile) = 0 Then Exit Sub


DirFile = "C:\Documents and Settings\Administrator\Desktop\" & strFile
If Len(Dir(DirFile)) <> 0 Then
  Workbooks.Open FileName:=strFile
    Range("A1:M150").Copy
    wbaudit.Activate
    Sheets("IMO EMV Holding").Activate
    Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Application.CutCopyMode = False
    Workbooks.Open FileName:=strFile
    ActiveWorkbook.Close
Else
 On Error Resume Next
 Set WB = Workbooks.Open(DirFile)
 On Error GoTo 0
 If WB Is Nothing Then MsgBox DirFile & " is invalid", vbCritical
End If
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,926
Members
453,767
Latest member
922aloose

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