+ Reply to Thread
Results 1 to 7 of 7

Thread: Use userform collected data to open a file

  1. #1
    Registered User
    Join Date
    09-30-2009
    Location
    Alexandria, VA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Use userform collected data to open a file

    Hi, I'm trying to use a userform to open a previously saved file to avoid having the users dig through the folder and maybe work on the wrong file.

    I have a file I have created to test if the code works, but the macro keeps giving me the reponse I set in case it doesn't find the file.

    The data for Account (Account1 and Subaccount1) is coming from a text box, as well as for category (Category1). The data for Company is an optionbutton frame with 4 options for the user to select from. Thanks in advance for any help!!!

    Private Sub CommandButton1_Click()
    
    Dim Company As String
    Dim Account As String
    Dim Category As String
    
    Account = Val(Account1.Text) & "_" & Val(SubAccount1.Text)
    Category = Val(Category1.Text)
    
    If Account1.Text = "" Then
        MsgBox "You must enter an account number."
        Unload UserForm2
        UserForm2.Show
        Exit Sub
    End If
    
    If SubAccount1.Text = "" Then
        MsgBox "You must enter a sub-account number."
        Unload UserForm2
        UserForm2.Show
        Exit Sub
    End If
    
    If Category1.Text = "" Then
        MsgBox "You must enter a category number."
        Unload UserForm2
        UserForm2.Show
        Exit Sub
    End If
    
    'Transfer company number
    If OptionCo10.Value = True Then Company = Val("10")
    If OptionCo30.Value = True Then Company = Val("30")
    If OptionCo56.Value = True Then Company = Val("56")
    If OptionCo11.Value = True Then Company = Val("11")
    
    'Open file
    
    If Dir("J:\Finance\xls\" & _
            Company & " - " & Account & " - " & Category & ".xls") <> "" Then
            
            Workbooks.Open "J:\Finance\xls\" & _
            Company & " - " & Account & " - " & Category & ".xls", ReadOnly:=False
            Windows("2010 Plan Builder.xls").Activate
            Application.DisplayAlerts = False
            ActiveWorkbook.Close
            Application.DisplayAlerts = True
    
    Else
    
    Dim Msg As String, Title As String
    Msg = "The file you have requested could not be located."
    Msg = Msg & vbNewLine & vbNewLine
    Msg = Msg & "Please make sure you have entered the correct information."
    MsgBox Msg, , Title
    
    End If
    
    End Sub
    Last edited by gophins; 10-29-2009 at 05:19 PM. Reason: Solved

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Use userform collected data to open a file

    Store the path & name in a hidden sheet
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    09-30-2009
    Location
    Alexandria, VA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Use userform collected data to open a file

    Roy, thanks for the response.

    I'm not quite sure I understand, do you mean I must transfer the data I collect in the userform to a cell and then it would find the file?

    Thank you.

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Use userform collected data to open a file

    I mant you could store the path in a worksheet then retrieve it with the code.

    can you attach a sample workbook
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Registered User
    Join Date
    09-30-2009
    Location
    Alexandria, VA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Use userform collected data to open a file

    Here is the first tab of the spreadsheet. The "Continue on a previously saved account" has the macro in question attached to it. I had removed other tabs but it should not have an impact.

    Thank you!
    Attached Files Attached Files

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Use userform collected data to open a file

    I can't really heck this but it looks like the name is incorrect try checking it by adding a messagebox

    'Open file
    MsgBox "J:\Finance\xls\" & _
            Company & " - " & Account & " - " & Category & ".xls"
    If Dir("J:\Finance\xls\" & _
            Company & " - " & Account & " - " & Category & ".xls") <> "" Then
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  7. #7
    Registered User
    Join Date
    09-30-2009
    Location
    Alexandria, VA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Use userform collected data to open a file

    Thanks!! It was a silly mistake and placing the msg box helped me catch it right away.
    Thank you very much!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0