Forum Discussion
Use a macro to import
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
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.
- Simon_PantonJul 10, 2023Copper Contributor
Hello there put in the corrections but then got another error Run time error 9 subscript out of range and the bit that was highlighted is below.
- Simon_PantonJul 10, 2023Copper ContributorForgot to mention the other thing the macro does is its pulled in the daata for the first file but copied it down 466 times to row 467. The next data from the next file only pulled in data from cells b2 and b4. not sure if this is related to the above error code. Please may I also thank you for your help with this.
Simon