I'm a beginner in VBA and have to work on some task to open a folder consists of scientific results in Excel files, select some cells based on a specific key from each Excel file and retrieve these data to the current workbook/worksheet in a sort of final table.
I'm getting this error
Subscript out of range (Error 9)
and I know the reason because it can't find the current worksheet to paste the data as required.
The current workbook named Task and current worksheet Output
Here's the edited code:
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook, current As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim sht As Worksheet
'set source workbook
Set current = ThisWorkbook
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
Set sht = wb.Worksheets(1)
' create an array with the keys' names
Dim arr(3) As String
Dim element As Variant
arr(0) = "aclr_utra1"
arr(1) = "aclr_utra2"
arr(2) = "aclr_eutra"
' get the last row in each worksheet
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row
'create two nested loops to retrieve the results with each key
For Each element In arr
' Retrieve and copy the matched results
For i = 35 To LastRow
If sht.Cells(i, 9).Value = CStr(element) Then
sht.Cells(i, 6).Copy 'BW
sht.Cells(i, 8).Copy 'Spec_symbol
' copy to the final sheet
erow = current.Worksheets("Output").Cells(85, 1)
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
Next element
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
I guess the problem now is that this line returns Nothing, however it opens the right worksheet and both myPath & myFile are correct!
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Set current = ThisWorkbook
. Also when you open the file then declare a variable for the worksheet likeDim sht as Worksheet
andSet sht = wb.Worksheets(1)
(Similiar for Target just with current. Also use wb and not activeWorkbook there.). Then everyCells, Range etc.
should be objectified to make sure it references to the right workbook i.esht.Cells(1,1).Value
. What iserow = current.Worksheets("Output").Cells(85, 1)
supposed to do?erow
is declared as an integer but its aRange
. And then you are using it as aRow
? – PlagonSet
after the wb has been opened. – PlagonMsgBox myPath & myFile
Is the path of the file ok? – paul bica