Pulling Data - If cannot find file

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Just need a bit of extra code to the below that says if the file is not found/doesnt exist then continue on and ignore, any ideas please?

Many thanks.

Code:
Public Sub Copy_Cells()


    Dim destcell As Range, r As Long
    Dim fileSpec As String, folderPath As String, fileName As String
    
    cellValue1 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("U6")
    cellValue2 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("U12")
      
    fileSpec = "C:\Users\jamesco\OneDrive for Business\PMO - Schedule 4 - Sharing best practice\2. CrossCountry Trains\Revenue\" & myValue & "\" & cellValue1 & ""
    fileSpec2 = "C:\Users\jamesco\OneDrive for Business\PMO - Schedule 4 - Sharing best practice\2. CrossCountry Trains\Revenue\" & myValue & "\" & cellValue2 & ""
 
    Set destcell1 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("C6")
    Set destcell2 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("D6")
    Set destcell3 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("C12")
    
    r = 0
    
    folderPath = Left(fileSpec, InStrRev(fileSpec, "\"))
    folderPath2 = Left(fileSpec, InStrRev(fileSpec2, "\"))
    fileName = Dir(fileSpec)
    fileName2 = Dir(fileSpec2)
    While Len(fileName) <> 0
        destcell1.Offset(r, 0).Value = GetCellValue(folderPath & fileName, "Forecast", "H12")
        destcell2.Offset(r, 0).Value = GetCellValue(folderPath & fileName, "Forecast", "H16")
        destcell3.Offset(r, 0).Value = GetCellValue(folderPath2 & fileName2, "Forecast", "G12")
        r = r + 1
        fileName = Dir
    Wend
      
End Sub


Private Function GetCellValue(ByVal workbookFullName As String, sheetName As String, cellsRange As String)


    Dim folderPath As String, fileName As String
    Dim arg As String
    
    folderPath = Left(workbookFullName, InStrRev(workbookFullName, "\"))
    fileName = Mid(workbookFullName, InStrRev(workbookFullName, "\") + 1)
    folderPath2 = Left(workbookFullName, InStrRev(workbookFullName, "\"))
    fileName2 = Mid(workbookFullName, InStrRev(workbookFullName, "\") + 1)
    arg = "'" & folderPath & "[" & fileName & "]" & sheetName & "'!" & Range(cellsRange).Address(True, True, xlR1C1)
    arg = "'" & folderPath2 & "[" & fileName & "]" & sheetName & "'!" & Range(cellsRange).Address(True, True, xlR1C1)
    Debug.Print arg
          
    'Execute Excel 4 Macro with argument to closed workbook
    
    GetCellValue = ExecuteExcel4Macro(arg)
    
End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,215,854
Messages
6,127,345
Members
449,379
Latest member
phyxius117

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