[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA Excel 2000 - File existing or not

Posted on 2011-03-25
3
Medium Priority
?
443 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 1000 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 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

834 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