VBA script to let users choose three spreadsheets to opened up as tabs in the original spreadsheet ?

netrixuser

Board Regular
Joined
Jan 21, 2019
Messages
77
Office Version
  1. 365
Platform
  1. Windows
I have a script that is working but I need to streamline it and make it more user friendly.
My code opens up three spreadsheets and copies them to tabs 2,3 and 4 in the original spreadsheet. It renames the tabs and does a bunch of formatting and a few Vlookups.

The original code to open up the three spreadsheets (reports) is below, it isn't probably the most economic/simplest way to achieve the goal, but it has worked up until now. The only caveat with my code is the three spreadsheet reports have to be renamed and placed in the user's root directory of their C: drive for it to work - which is a tad clumsy !
With help from this site, I have been able to insert some code that prompts the user to select the file(s) to open but I cannot tweak it so that is saves the newly opened spreadsheet in the next tab of the original spreadsheet - my plan was to have the prompt to select the files pop up three times, to select the three spreadsheets etc.

Here is the part of my original code that opens up the three reports:[the original spreadsheet has one workbook in it named "Index"]
Code:
 Application.ScreenUpdating = False

    'This script will open the three reports
    Sheets("Index").Select
    ControlFile = ActiveWorkbook.Name
    Workbooks.Open Filename:="C:\Active MRL"
    ActiveSheet.Name = "Accsys Report"
    Sheets("Accsys Report").Copy After:=Workbooks(ControlFile).Sheets(1)
    Windows("Active MRL").Activate
    ActiveWorkbook.Close SaveChanges:=False
    Windows(ControlFile).Activate




    Sheets("Index").Select
    ControlFile = ActiveWorkbook.Name
    Workbooks.Open Filename:="C:\Fonetic Report"
    ActiveSheet.Name = "Fonetic Report"
    Sheets("Fonetic Report").Copy After:=Workbooks(ControlFile).Sheets(1)
    Windows("Fonetic Report").Activate
    ActiveWorkbook.Close SaveChanges:=False
    Windows(ControlFile).Activate
    
    
    Sheets("Index").Select
    ControlFile = ActiveWorkbook.Name
    Workbooks.Open Filename:="C:\Cognia Report"
    ActiveSheet.Name = "Cognia Report"
    Sheets("Cognia Report").Copy After:=Workbooks(ControlFile).Sheets(1)
    Windows("Cognia Report").Activate
    ActiveWorkbook.Close SaveChanges:=False
    Windows(ControlFile).Activate

After this has run I have the original spreadsheet with three new tabs named: Cognia Report, Fonetic Report and Accsys Report (in that order, from left to right)

The code below is what I am playing with to prompt the user to select the file (the help I got was to force the open file box to go to the user's download folder) but I cannot, yet, work out how to run it three times ending up with my original spreadsheet plus the three new workbooks renamed etc:

So, I started with this, which has a message box to tell the user which report to open before the open file box appears:
Code:
Dim TargetFile As Variant

    ChDir "C:\Users\" & Environ$("UserName") & "\Downloads"
    MsgBox "Please Select the Fonetic Report", , "SELECT THE FONETIC REPORT"
' Open dialogue box to browse for file
        TargetFile = Application.GetOpenFilename _
        (Title:="Please choose a file to open", _
        FileFilter:="Excel Files *.xls* (*.xls*),")


' If no file is selected then...
        If TargetFile = False Then
            ' Display message box with an error
            MsgBox "No file selected.", vbExclamation, "Sorry!"
            ' Exit the macro
        Exit Sub
' Else if a valid file is selected then...
        Else
            ' Open the selected workbook
            Workbooks.Open Filename:=TargetFile
        End If
    
End Sub

I would like the workbook that opens to be saved in the original spreadsheet, and this process repeated another two times to open the remaining two reports up.

Any help most gratefully received !!

Regards
NetrixUser
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top