I am interested if I write a VBA code can it run between 2 files?

Meko1

New Member
Joined
Oct 20, 2020
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hello I know that macros can be run between several excel files, but can it run data from one file into another just by running macro?
Lets call 2 files "2020" and "2021". So if I needed to copy-paste data from "2020" to "2021" I would use =file"2021 cellA1"!.... something like this, but since my data range is big and massy I want to make code which will do specific action...

I hope I made it clear.
Thanks in advance.
Hope to hear from you soon.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You just need to identify the workbooks and the sheets in the workbooks that you want to work with. As an example:
VBA Code:
Sub xAmple()
Dim sh1 As Worksheet, sh2 As Worksheet  'Delare the variables you intend to use
Set sh1 = Workbooks("2020.xlsx").Sheets(1) 'substitute sheet name for index number - This puts the source sheet in a variable
Set sh2 = Workbooks("2021.xlsx").Sheets(1) 'substitute sheet name for indes number = This puts the destination sheet in a variable
sh1.Range("A1:F50").Copy
sh2.Range("A1").PasteSpecial xlPasteValuesAndNumberFormat 'assumes source sheet had formulas not to be copied.
End sub

Conditions: Both workbooks would need to be open at runtime. VBA will produce an error message if the copy range includes only part of merged cells. This code assumes that there are formulas in the source sheet, so the PasteSpecial method is used. When you use the Workbooks() syntax, the sheet must also be specified, unlike the Windows() syntax. By putting the sheet and its parent workbook into a variable, the need to spell out both each time is eliminated and only the variable need be used. If you try to use the variable like 'Workbooks("2020.xlsx").sh1', it will produce an error. Same with worksheet, because both are already included in the variable and trying to combine them with the variable causes the error.

There are tutorials on the web that explain all this in great detail if you search for them. Or you could use the macro recorder to get the basic code then ask for help to streamline it.
Regards, JLg
 
Upvote 0
Solution

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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