Forum Discussion
Use a macro to import
Hello all
I am trying to write/record a macro to import into a single sheet date from a number of different sheets.
All the required data in the source files (325 in total) are in exactly the same cells (D1 and D2)
In the destination files the imported data needs to go into A1 B1, A2 B2 etc where the "A"s are the data from source files cells D1 and the "B"s are from source cells D2 and each row therefore contains data from the same file/cells D1 and D2.
The version of excel I am using is Microsoft Excel for Mac under microsoft 365 subscription.
If any body can help thank you
Simon
5 Replies
- NikolinoDEGold Contributor
To import data from multiple source files into a single sheet using a macro in Excel, you can use the following code as a starting point:
Vba Code (untested):
Sub ImportDataFromFiles() Dim SourceFolder As String Dim FileName As String Dim DestinationSheet As Worksheet Dim LastRow As Long Dim CurrentRow As Long ' Set the folder path where the source files are located SourceFolder = "C:\Path\to\Source\Folder\" ' Replace with your actual folder path ' Set the destination sheet where the data will be imported Set DestinationSheet = ThisWorkbook.Sheets("DestinationSheetName") ' Replace "DestinationSheetName" with the actual sheet name ' Clear previous data in the destination sheet DestinationSheet.UsedRange.Clear ' Loop through each file in the source folder FileName = Dir(SourceFolder & "*.xlsx") ' Modify the file extension if necessary CurrentRow = 1 ' Starting row in the destination sheet Do While FileName <> "" ' Open the source file Workbooks.Open SourceFolder & FileName ' Copy data from source file to destination sheet With Workbooks(FileName).Sheets(1) ' Modify the sheet index if necessary LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row ' Copy data from source cells D1 and D2 to destination columns A and B DestinationSheet.Range("A" & CurrentRow & ":A" & CurrentRow + LastRow - 1).Value = .Range("D1").Value DestinationSheet.Range("B" & CurrentRow & ":B" & CurrentRow + LastRow - 1).Value = .Range("D2").Value End With ' Close the source file without saving Workbooks(FileName).Close SaveChanges:=False ' Move to the next row in the destination sheet CurrentRow = CurrentRow + LastRow FileName = Dir Loop End Sub
Before running the macro, you need to update the SourceFolder variable with the actual folder path where your source files are located. Also, update the DestinationSheet variable with the name of the destination sheet where you want to import the data.
This macro loops through each file in the source folder, opens the file, copies the data from cells D1 and D2 in the first sheet, and pastes it into columns A and B of the destination sheet.
The data is appended row by row, so each file's data will be in a separate block in the destination sheet.
Make sure to modify the file extension and the sheet index in the code if your source files have a different file extension or if the data is located in a different sheet within the source files.
Before running the macro, save your workbook and test it with a few sample files to ensure it works as expected.
To run the macro in Excel for Mac, follow these steps:
- Open your Excel workbook.
- Press Option + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, insert a new module by clicking on "Insert" and selecting "Module."
- Copy and paste the code into the new module.
- Modify the SourceFolder and DestinationSheet variables with the appropriate folder path and sheet name.
- Close the VBA editor.
- Press Option + F8 to open the "Macro" dialog box.
- Select the "ImportDataFromFiles" macro from the list and click "Run."
The macro should now execute and import the data from the source files into the designated sheet in your Excel workbook.
Please note that if your source files have a different file extension or the data is located in a different sheet within the files, you may need to modify the code accordingly.
To be sure that the code is behaving the way you want, it would be helpful to have the file (without any sensitive data) to run tests on rather than reconstructing one yourself. Information such as Excel version, file extension, operating system, storage medium would be of additional help. The text, steps and functions were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
- Simon_PantonCopper Contributor
Hello and thank you so much for your reply
I modified your code as advised to the following
Sub ImportDataFromFiles()
Dim SourceFolder As String
Dim FileName As String
Dim DestinationSheet As Worksheet
Dim LastRow As Long
Dim CurrentRow As Long
SourceFolder = "/Users/simonpanton/Library/CloudStorage/OneDrive-Personal/MSc/MSC Project/Raw data/Freshwater/”"
Set DestinationSheet = ThisWorkbook.Sheets("sample sites2.xlsx") DestinationSheet.UsedRange.Clear
' Loop through each file in the source folder
FileName = Dir(SourceFolder & "*.xlsx") ' Modify the file extension if necessary
CurrentRow = 1 ' Starting row in the destination sheet
Do While FileName <> ""
' Open the source file Workbooks.Open SourceFolder & FileName
' Copy data from source file to destination sheet
With Workbooks(FileName).Sheets(1) ' Modify the sheet index if necessary
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
' Copy data from source cells f1 and f2 and B2 and B4 to destination columns A and B and C and D
DestinationSheet.Range("A" & CurrentRow & ":A" & CurrentRow + LastRow - 1).Value = .Range("f1").Value
DestinationSheet.Range("B" & CurrentRow & ":B" & CurrentRow + LastRow - 1).Value = .Range("f2").Value
DestinationSheet.Range("C" & CurrentRow & ":C" & CurrentRow + LastRow - 1).Value = .Range("b2").Value
DestinationSheet.Range("d" & CurrentRow & ":d" & CurrentRow + LastRow - 1).Value = .Range("b4").Value
End With
' Close the source file without saving Workbooks(FileName).
Close SaveChanges:=False
' Move to the next row in the destination sheet
CurrentRow = CurrentRow + LastRow
FileName = Dir Loop End Sub
when I run this I get an error message "Subscript out of range (Error 9)"
The script above highlighted in red is highlighted.
Im not familiar with actually writing macros so not sure what should be left in or out from your original script so all I did was correct the cells to be imported from and added a couple extra.
also in Excel for Mac when I pre option + F11 it opens up system settings not the macro dialog box.
Also does the destinationsheetname refer to the destination file or sheet with the file and does this file need to be marco enabled?
sorry for all these questions and thank you.
regards Simon
- NikolinoDEGold Contributor
It seems there are a few issues with the modified code you provided. Here are the corrections and clarifications:
- Subscript out of range error: The "Subscript out of range" error occurs when the script is trying to access a workbook or worksheet that doesn't exist. In your code, you are trying to access a sheet named "sample sites2.xlsx," which is incorrect. The sheet should be referred to by its name, not the name of the file it belongs to. Modify the line to refer to the sheet name:
Vba Code:
Set DestinationSheet = ThisWorkbook.Sheets("SheetName") ' Replace "SheetName" with the actual name of the destination sheet in your workbook
- acro dialog box in Excel for Mac: On Mac, the shortcut to open the VBA editor is Option + F11. However, if you're unable to open the VBA editor using the shortcut, you can try accessing it through the "Developer" tab. To enable the "Developer" tab in Excel for Mac, go to Excel Preferences -> Ribbon & Toolbar, and check the box next to "Developer" under the "Customize the Ribbon" section. Then, you can access the VBA editor by clicking on the "Developer" tab and selecting "Visual Basic."
- Destination sheet and file: The DestinationSheet variable refers to the destination worksheet within the same workbook where you are running the macro. Make sure the sheet name is correct. The workbook where the macro is saved does not need to be macro-enabled, but it should be open when you run the macro.
Here is the corrected version of the code:
Vba Code:
Sub ImportDataFromFiles() Dim SourceFolder As String Dim FileName As String Dim DestinationSheet As Worksheet Dim LastRow As Long Dim CurrentRow As Long SourceFolder = "/Users/simonpanton/Library/CloudStorage/OneDrive-Personal/MSc/MSC Project/Raw data/Freshwater/" Set DestinationSheet = ThisWorkbook.Sheets("SheetName") ' Replace "SheetName" with the actual name of the destination sheet in your workbook DestinationSheet.UsedRange.Clear FileName = Dir(SourceFolder & "*.xlsx") ' Modify the file extension if necessary CurrentRow = 1 ' Starting row in the destination sheet Do While FileName <> "" Workbooks.Open SourceFolder & FileName With Workbooks(FileName).Sheets(1) ' Modify the sheet index if necessary LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row DestinationSheet.Range("A" & CurrentRow & ":A" & CurrentRow + LastRow - 1).Value = .Range("F1").Value DestinationSheet.Range("B" & CurrentRow & ":B" & CurrentRow + LastRow - 1).Value = .Range("F2").Value DestinationSheet.Range("C" & CurrentRow & ":C" & CurrentRow + LastRow - 1).Value = .Range("B2").Value DestinationSheet.Range("D" & CurrentRow & ":D" & CurrentRow + LastRow - 1).Value = .Range("B4").Value End With Workbooks(FileName).Close SaveChanges:=False CurrentRow = CurrentRow + LastRow FileName = Dir Loop End Sub
Make sure to replace "SheetName" with the actual name of the destination sheet in your workbook. Also, ensure that the source folder and file paths are accurate.
Once you have corrected the code, run the macro again, and it should import the data from the specified files into the destination sheet. The text, steps and code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.