Question about VBA

Anri

New Member
Joined
May 21, 2024
Messages
3
Office Version
  1. 365
Dears ,

I am new to the VBA language in Excel. I have a problem and I want to solve it using this language.
How can I link three Excel files into one file, or in other words, I want the data of the three Excel files to be transferred to one file?

Regards,
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If you are new to VBA, then you will need to learn more about the language before any description of how to do that will have any meaning for you. If you have any programming experience with other programming languages, then climbing the learning curve will be faster.

A good primer for VBA newbies.

There are also a number of good introductory VBA videos on youtube.

Good luck.
 
Upvote 0
The most common approach from a VBA standpoint is to adapt something like:
Set wb = Workbooks.Open(filePath)
Where wb is a variable declared as workbook and filePath is a variable declared as string. With this setup, you can manipulate the opened workbook's worksheets to get the data you need. Once you have the data, you can close the file. For example, if you wanted to extract the first cell of the first sheet of the newly opened workbook into cell F1 of the first sheet of the current workbook, you'd do something like this:
VBA Code:
Sub test()
    Dim filePath As String
    filePath = "C:\someFile.xlsx"
    
    Dim wb As Workbook
    Set wb = Workbooks.Open(filePath)
    
    ThisWorkbook.Sheets(1).Range("F1") = wb.Sheets(1).Range("A1")
    wb.Close
End Sub

Since you need 3 files, you can use a loop or write the 3 file paths in the code, but the most efficient way is to just make functions that can handle this task dynamically.
 
Upvote 0
If you are new to VBA, then you will need to learn more about the language before any description of how to do that will have any meaning for you. If you have any programming experience with other programming languages, then climbing the learning curve will be faster.

A good primer for VBA newbies.

There are also a number of good introductory VBA videos on youtube.

Good luck.
Thank you, I appreciate that very much. I really need to learn a lot in this language, but I don’t know where to start 😅
 
Upvote 0
The most common approach from a VBA standpoint is to adapt something like:
Set wb = Workbooks.Open(filePath)
Where wb is a variable declared as workbook and filePath is a variable declared as string. With this setup, you can manipulate the opened workbook's worksheets to get the data you need. Once you have the data, you can close the file. For example, if you wanted to extract the first cell of the first sheet of the newly opened workbook into cell F1 of the first sheet of the current workbook, you'd do something like this:
VBA Code:
Sub test()
    Dim filePath As String
    filePath = "C:\someFile.xlsx"
   
    Dim wb As Workbook
    Set wb = Workbooks.Open(filePath)
   
    ThisWorkbook.Sheets(1).Range("F1") = wb.Sheets(1).Range("A1")
    wb.Close
End Sub

Since you need 3 files, you can use a loop or write the 3 file paths in the code, but the most efficient way is to just make functions that can handle this task dynamically.

Thank you very much Edgar. It worked. Can I make it open for me the location of the three files and I choose them, for example, the October, November or December file, but I want to continue opening the selection option over the years?
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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