Macro to Open File with Variable Name and Copy Worksheet

brezinst

New Member
Joined
Oct 13, 2009
Messages
30
I need to write a macro that opens up a file with the name "<mm-dd-yy> Positions Insight" from a directory C:\Positions\ and copy a worksheet "Sheet1" and Paste Special Values into my original workbook called "MarketValues_<yyyymmdd>" on a sheet called "raw". Could someone please assist me in doing this? I can't seem to find an example anywhere.

Thanks so much in advance.

Todd</yyyymmdd></mm-dd-yy>
 

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

brezinst

New Member
Joined
Oct 13, 2009
Messages
30
Sorry, the file I'm opening is mm-dd-yy Positions Insight and the file I'm adding it too is MarketValues_yyyymmdd

The dates will always both be today.

Thanks
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Hello and welcome to MrExcel.

Try this - the code goes in the MarketValues... workbook

Code:
Sub Cpy()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Positions\" & Format(Date, "mm-dd-yy") & "Positions Insight.xls")
Sheets("Sheet1").UsedRange.Copy
ThisWorkbook.Sheets("raw").Range("A1").PasteSpecial Paste:=xlPasteValues
wb.Close savechanges:=False
End Sub
 

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,675
Try this:

Code:
Sub CopyAndPasteValues()

Dim WB1 As Workbook
Dim WB2 As Workbook

Set WB1 = ThisWorkbook
Sheets("Raw").Activate
'Finds the last row on Column A and selects the first empty cell below it
Range("A65536").End(xlUp).Offset(1, 0).Select

'Opens the workbook "Positions Insight.xls":
Workbooks.Open "C:\Positions\Positions Insight.xls"

Set WB2 = ActiveWorkbook
'Copies the A1 CurrentRegion:
Sheets("Sheet1").Range("A1").CurrentRegion.Copy

'Goes back to orginal workbook and pastes the values
WB1.Activate
ActiveCell.PasteSpecial xlValues

'Closes the workbook "Positions Insight.xls" without saving:
WB2.Close False
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,295
Messages
5,600,789
Members
414,405
Latest member
Zaurb

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
Top