Solved

VBA Excel 2000 - File existing or not

Posted on 2011-03-25
3
436 Views
Last Modified: 2012-05-11
Dear Experts,

I have a macro which assign a certain file name to a string variable, based on that after it opens it

Dim FileNev As String
Dim FileNevWorkbook As Workbook
FileNev = "D:\PR\Conv " & FileNap & "." & modMilyenHonap & ".xls"
Set FileNevWorkbook = Workbooks.Open(FileNev)

But sometimes it happens that the file does not exist, and the opening attempt brings error. Is there way by VBA to check whether that file exist and if not then bringing a MsgBox instead of the open?

thanks,
Zsolt
0
Comment
Question by:csehz
3 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 250 total points
ID: 35214398
something like this

hth
Dave
Sub testfile()
    If Len(Dir("D:\PR\Conv " & FileNap & "." & modMilyenHonap & ".xls")) > 0 Then
        Set FileNevWorkbook = Workbooks.Open(FileNev)
    Else
        MsgBox "File not found."
    End If
End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:Kannan K
Kannan K earned 250 total points
ID: 35214408
Hi,

Please take the below function.

KK,
Public Function FileFolderExists(strFullPath As String) As Boolean
    On Error GoTo EarlyExit
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    
EarlyExit:
    On Error GoTo 0

End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:csehz
ID: 35214433
Thanks very much for the two different approaches
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question