VBA code to open a file

mjhogan2

New Member
Joined
Oct 28, 2019
Messages
2
Hi,

I m new to VBA coding - I am looking for some guidance to the following:

I would like to use a workbook to search for a folder in explorer, open the desired workbook in that folder, then slect a range of data from the selected workbook and store it in a new work book. I need something that will work on what ever workbook in the file I choose.

I hope this makes sense - I have been trying to find some online tutes online but nothing suits what I need

Cheers,

Matt
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
So you just want to open a closed workbook.
You then want to go through possibly many sheets to find the range you want to copy and paste this in a new workbook.
Or is the sheet to copy from and the range to copy known? (hard coded like "Sheets("Sheet3").Range("C12:F25").Copy")
 
Upvote 0
Hi,
To clarify - I want to:
1. Open a workbook from a file location (from a number of workbooks stored there)
2. From the selected workbook - import data (from a known range) to new workbook
3. Then I will perform some calculations following on.

So - the sheet in the workbook will aways be sheet 1, and the range will also be known.
I am just trouble importing the data fom the original fileto a new sheet
 
Upvote 0
Try this.
Code:
Sub Open_Workbook_Single_Select()
    Dim myFile As String, wbS As Workbook
    Application.ScreenUpdating = False
    Sheets.Add(, Sheets(Sheets.Count)).Name = "Temp"
    myFile = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
    MultiSelect:=False)
        Set wbS = Workbooks.Open(myFile)
            wbS.Sheets("Sheet1").Range("A3:A30").Copy ThisWorkbook.Sheets("Temp").Range("A1")    '<---- Change the ranges as required
        wbS.Close False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
After the line
Code:
wbS.Close False
insert these two lines
Code:
Sheets("Temp").Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & "Temp", FileFormat:=xlOpenXMLWorkbook
This saves the new Workbook as an .xlsx type file in the same Folder where your Workbook that has the code in it is saved.

If you want to save it in .xlsm format, use
Code:
FileFormat:=xlOpenXMLWorkbookMacroEnabled
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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