Results 1 to 7 of 7

Use userform collected data to open a file

Threaded View

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

    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

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.6.0 RC 1