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>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,016
Members
448,936
Latest member
almerpogi

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