+ Reply to Thread
Results 1 to 3 of 3

From userform, open file and run macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    From userform, open file and run macro

    I have a userform which begins with a "Find File" button.
    Once the user has found the excel sheet he wishes to use (which has a macro on it that does a specific thing) the "Find File" macro opens the worksheet. Once this is done, how can I set it to run the macro on that newly opened worksheet?

    Private Sub CommandButton1_Click()
    ChDrive "D:\"
    ChDir "D:\Documents"
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    If FileToOpen = False Then
    MsgBox "No file specified. You must select a file for the macro to run", vbExclamation, "Error"
    Exit Sub
    Else
    Workbooks.Open Filename:=FileToOpen
    End If
    
    End Sub
    I dont know if i can set a Dim or something to do this or also if i need to set the specific macro name but assume the macro name is macro1 if that is the case

    Thanks,
    Penfold

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: From userform, open file and run macro

    Private Sub CommandButton1_Click()
    ChDrive "D:\"
    ChDir "D:\Documents"
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    If FileToOpen = False Then
    MsgBox "No file specified. You must select a file for the macro to run", vbExclamation, "Error"
    Exit Sub
    Else
    Workbooks.Open Filename:=FileToOpen
    End If
    
    Your Macro Name Here
    
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: From userform, open file and run macro

    Apologies for the late reply, it seems I made a mistake in my question. I will explain better here:

    I have 2 files but want to make a third which i will call REPORT for now. the other two are called MACRO and IMPORT.

    open MACRO
    press a button on macro that opens a dialog box allowing me to select IMPORT
    a macro then creates a new workbook called IMPORT

    leaving me with two variables that i can switch between with the codes:

    IMPORT.Activate
    and
    REPORT.Activate

    apologies for the bad question before. I hope this makes more sense.

+ 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