Need VBA to open files based on file name

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
Hello,

I need a VBA code to open a file in a specific file path, copy the set of the data, paste into another workbook then close that file.

Here's the following criteria:

File path: S:\NPH Accounting\NPH Accounting Department\Allocations\2016\Employee Allocation

- The files in this folder vary month over month (for example "01 Jan Employee Allocations 2016", "02 Feb Employee Allocations 2016", etc)

How can I code this to open the latest file and paste it into my workbook we will call "workbook A"?

Thank you in advance.
 
change this block of code

Code:
Month.Activate
Activesheet.cells.select
Selection.Copy
Main.Activate
Month.Sheets("Sheet5").Cells.Select
Main.Selection.Paste

I'm having trouble with this block because I don't know how to go back to the original file since we are using "active sheets" and the active sheet at that stage would be the workbook I'm opening and copying data from.

How can I set my file I'm pasting data in so I can set it as something and make it go there instead?
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This code is working fine for me... I'll put my comments to simplify it for you.

Code:
'the below is the workbook where the data will be copied from it is getting activated
Month.Activate
'now when the above workbook is activated the data from its active sheet will be selected and copied
Activesheet.cells.select
Selection.Copy
'now the sheet where you are running this macro from is being activated
Main.Activate
'you must now select and change focus to the sheet where you want to copy your data
Main.sheets("Sheet5").select
Main.Sheets("Sheet5").Cells.Select
Main.Selection.Paste
 
Last edited:
Upvote 0
But when you use:
Code:
'you must now select and change focus to the sheet where you want to copy your data
Main.sheets("Sheet5").select
Main.Sheets("Sheet5").Cells.Select
Main.Selection.Paste
It is in a different workbook, not within the same workbook/ So there is no "Sheet5" on the active workbook.
 
Upvote 0
Ok.. Can you tell me what's the name of the sheet on your main workbook where you want the data to finally be pasted? And also the name of the sheet where you intend to copy this data from.
 
Upvote 0
The name of the sheet on the main workbook is "Employee Allocations List" or "Sheet5".

The name of the sheet I'm copying data from is "Employee Allocations List" or "Sheet1".

Thank you so much.
 
Upvote 0
Based on the given sheet names the below code should work fine.

The below code is assigned to open the workbook specified in Range ("A1") and refer to sheet 1 to copy the data then take it back to the main workbook and paste it on sheet5

Code:
Sub UpdateEmployeeAllocationList()


Dim Main As Workbook
Dim Month As Workbook


Set Main = Thisworkbook
file = Range("A1").value
Set Month = Workbooks.Open("S:\NPH Accounting\NPH Accounting Department\Allocations\2016\Employee Allocation\" & file & " Employee Allocations 2016.xlsx")



'the below is the workbook where the data will be copied from it is getting activated
Month.Activate
Month.sheets("Sheet1").select
'now when the above workbook is activated the data from its active sheet will be selected and copied
[FONT=Verdana]Month.sheets("Sheet1")[/FONT].cells.select
Selection.Copy
'now the sheet where you are running this macro from is being activated
Main.Activate
'you must now select and change focus to the sheet where you want to copy your data
Main.sheets("Sheet5").select
Main.Sheets("Sheet5").Cells.Select
Main.Selection.Paste
Month.Close False
End Sub
 
Last edited:
Upvote 0
Based on the given sheet names the below code should work fine.

The below code is assigned to open the workbook specified in Range ("A1") and refer to sheet 1 to copy the data then take it back to the main workbook and paste it on sheet5

Code:
Sub UpdateEmployeeAllocationList()


Dim Main As Workbook
Dim Month As Workbook


Set Main = Thisworkbook
file = Range("A1").value
Set Month = Workbooks.Open("S:\NPH Accounting\NPH Accounting Department\Allocations\2016\Employee Allocation\" & file & " Employee Allocations 2016.xlsx")



'the below is the workbook where the data will be copied from it is getting activated
Month.Activate
Month.sheets("Sheet1").select
'now when the above workbook is activated the data from its active sheet will be selected and copied
[FONT=Verdana]Month.sheets("Sheet1")[/FONT].cells.select
Selection.Copy
'now the sheet where you are running this macro from is being activated
Main.Activate
'you must now select and change focus to the sheet where you want to copy your data
Main.sheets("Sheet5").select
Main.Sheets("Sheet5").Cells.Select
Main.Selection.Paste
Month.Close False
End Sub


This is giving me a subscript out of range error message.
 
Upvote 0
Which line are you getting the error on? Can you de-bug this manually and check?

Try to replace this block of code

Code:
Main.Selection.Paste

[COLOR=#222222][FONT=Verdana]

with

Code:
Activesheet.paste
[/FONT][/COLOR]
 
Last edited:
Upvote 0
I figured it out. I changed name of sheet to match. However because of selecting so many times I think the VBA doesn't remember the original selection it copied.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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