Results 1 to 7 of 7

"OpenFile" Code Cleanup

Threaded View

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    "OpenFile" Code Cleanup

    Hi all,
    I am currently tweeking my VBA for work. In one macro, I've prompted the user to enter the name of the file to open. I've then passed the name into a function that opens it if it is there or display an error message if it is not. I works just fine but I was hoping that a VB pro could tell me if I've done it right. Here is the Function:

    Private Function OpenFile(ByVal fName As String) As Boolean
        Dim bool As Boolean
        Dim wbSource As Workbook
        Dim MyPath As String
        bool = True
        MyPath = ActiveWorkbook.Path
    
        On Error Resume Next
        Workbooks.Open (MyPath & "\" & fName)
        If Workbooks(fName) Is Nothing Then
            MsgBox "File does not exist" & vbNewLine & "Please re-run the the update and enter the proper" & vbNewLine _
                 & "file name", vbOKOnly, "FILE NOT FOUND"
            bool = False
            OpenFile = bool
            Exit Function
        End If
        OpenFile = bool
    End Function
    Once this is passed and returned to my other macros, I have this:
    If OpenFile(fName) = False Then Exit Sub
    Is this okay?
    Any insight will be truly appreciated.

    Regards:
    Last edited by Mordred; 09-23-2010 at 12:29 PM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1