VBA Modify code to say "If" file exists

LNG2013

Active Member
Joined
May 23, 2011
Messages
465
I have the below code I need to modify to add an "If" the file goal.xlsx exists.
If it doesn't - do nothing. If it does import it.





VBA Code:
Sub AOpenWB()

Application.ScreenUpdating = False
'Some Clean up first Clears import page


    Sheets("Import").Select
    Cells.Select
    Selection.ClearContents
    
  'Opens IEP GOAL data from same goal.
  Workbooks.Open "L:\Database\Goals\DropOff\goals.xlsx"
  

    Windows("goals.xlsx").Activate
    Cells.Select
    Selection.Copy
    Windows("GoalConditioner.xlsm").Activate
    Cells.Select
    ActiveSheet.Paste
    
    
      Workbooks("goals.xlsx").Close SaveChanges:=False
      Windows("GoalConditioner.xlsm").Activate
    

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Have a look at the methods used here to check for a file's existence.
 
Upvote 0
This code may help you
VBA Code:
Sub CheckFolderExists ()

Dim strFolderName As String
Dim strFolderExists As String

    strFolderName = "C:\Users\Nikola\Desktop\VBA articles\Test Folder\"
    strFolderExists = Dir(strFolderName, vbDirectory)

    If strFolderExists = "" Then
        MsgBox "The selected folder doesn't exist"
    Else
        MsgBox "The selected folder exists"
    End If

End Sub
We first assi
 
Upvote 0
This code may help you
VBA Code:
Sub CheckFolderExists ()

Dim strFolderName As String
Dim strFolderExists As String

    strFolderName = "C:\Users\Nikola\Desktop\VBA articles\Test Folder\"
    strFolderExists = Dir(strFolderName, vbDirectory)

    If strFolderExists = "" Then
        MsgBox "The selected folder doesn't exist"
    Else
        MsgBox "The selected folder exists"
    End If

End Sub
We first assi
It looks like they want to check for the FILE, not the FOLDER.
Code is very similiar, but has a few key differences.
The link I posted shows the exact code needed right at the top.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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