Performing simple macro

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
Hi,

I have a button with an assigned macro to it. I want the user to be able to click the button and have an option to choose an excel file from your desktop/folder/etc. and create a new sheet to insert the excel file. Then have the macro perform it's function on the excel file that the user clicked. How would I go about this? Thanks.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this
Code:
Sub Button1_Click()
    Dim fNameAndPath As Variant, wb As Workbook, ws As Worksheet
    fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS*), *.XLS*", Title:="Select File To Be Opened")
    If fNameAndPath = False Then Exit Sub
    Set wb = Workbooks.Open(fNameAndPath)
    Set ws = wb.Sheets.Add(before:=wb.Sheets(1))
    
    'do other stuff...
End Sub

Within the same procedure you can refer to the new sheet with ws and the opened workbook with wb

 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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