that code works great if i run it on the current worksheet but i'm running it from a different excel file. What i am trying to accomplish is to copy a range of cells from a closed file into the current worksheet. All my variables are correct because i've debug.printed them. its only the line:
first_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_SUPPLIER_CHARGE).Address
that gives me a run-time error 9. Subscripts out of range.
TOTAL_SUPPLIER_CHARGE refers to cell C34 and TOTAL_PROFIT refers to cell E34.
these cell values may change if the user inserts a new line therefore the name lists have to be used.
the variable FileNamesList(i) prints the correct file name ie: test1.xls
first_range and second_range are strings
For i = 1 To UBound(FileNamesList)
first_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_SUPPLIER_CHARGE & ":" & TOTAL_PROFIT).Address
second_range = Workbooks("FileNamesList(i)").ActivateRange(TOTAL_PROFIT).address
GetValuesFromAClosedWorkbook UserDirectory, FileNamesList(i), "Main", first_range & ":" & second_range, i
Cells(i + 4, 2).Formula = FileNamesList(i)
Next i
Sub GetValuesFromAClosedWorkbook(fPath As String, _
fName As Variant, sName, cellRange As String, counter As Long)
Dim rownumber As Long
Debug.Print fPath, fName, sName, cellRange, counter
rownumber = 4 + counter
With ActiveSheet.Range("C" & rownumber & ":E" & rownumber)
.FormulaArray = "='" & fPath & "\[" & fName & "]" _
& sName & "'!" & cellRange _
.Value = .Value
End With
End Sub
Bookmarks