Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA Excel 2000 - File existing or not

Posted on 2011-03-25
3
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

618 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