+ Reply to Thread
Results 1 to 7 of 7

"OpenFile" Code Cleanup

Hybrid 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---

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: "OpenFile" Code Cleanup

    Maybe like this:

    Sub TestIt()
        Dim wkb As Workbook
        
        Set wkb = OpenFile("Bob.xls")
        If wkb Is Nothing Then Exit Sub
        
        ' carry on ...
    End Sub
    
    Function OpenFile(ByVal sFile As String) As Workbook
        If Len(Dir(sFile)) Then
            Set OpenFile = Workbooks.Open(ThisWorkbook.Path & "\" & sFile)
        Else
            MsgBox Prompt:="File does not exist!" & vbLf & vbLf & _
                           "Please rerun the the update and enter the proper filename", _
                   Buttons:=vbOKOnly, _
                   Title:="FILE NOT FOUND"
        End If
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: "OpenFile" Code Cleanup

    A minor point: you have a double "the" in your msgbox message.

    "Please rerun the the update and enter the proper filename"

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

    Re: "OpenFile" Code Cleanup

    Haha, thanks for point out the 'the the'. I'll take care of it.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: "OpenFile" Code Cleanup

    Wouldn't this be simpler and replace the whole function ?

    If Dir(fName) = "" Then Exit Sub
    workbooks.open fName



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

    Re: "OpenFile" Code Cleanup

    Quote Originally Posted by snb View Post
    Wouldn't this be simpler and replace the whole function ?

    If Dir(fName) = "" Then Exit Sub
    workbooks.open fName
    This goes straight to Exit Sub, even thought the file is there.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: "OpenFile" Code Cleanup

    I don't think so.

+ Reply to Thread

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