1

(Excel 2010) I'm trying to grab specific lines from a variety of "target" workbooks in different but similar folders. I have found that I am able to pull this data when the source ("LM", the workbook the code is executing in, and that I want to pull the data to) and target workbooks are in the same folder without opening the target's workbook, but when they are in different locations (as they will be in practice) I receive a "Subscript out of range" error for the

LM.Worksheets("Sheet1").Range("B" & i + 1 & ":G" & i + 1).Value = _
          Workbooks(filename).Worksheets("Sheet1").Range("B6:G6").Value

line. I have tried:

  • Using every variant & combination on pathname, dirname & filename, etc. as the argument for the latter Workbooks(). I have also had it MsgBox me the pieces and whole of pathname and filename to look at, which are error-free.

  • Replacing the latter Workbooks(filename) with a workbook variable (lets call it Targ), like LM (which works fine)

  • Changing the path with ChDir and ChDrive (& I have confirmed that the CurDir() is in fact the target directory when this is running) and doing the above

  • Using ThisWorkbook instead of LM for the call

  • Basically every permutation of the above ideas

Here is a stripped-down (because confidential stuff was in there) version of the code (which works fine if I un-comment Workbooks.Open and Workbooks.Close, but I want a more efficient method since this is a busy network and people are in-and-out of these files all the time. The fact that I can do this without opening the files if they're in the same folder tells me I'm onto something...)

Sub Import()
    Dim directory As String, fileName As String, LM As Workbook, i as Integer
    Set LM = Workbooks("LM.xlsm")

    i = 1

    Dim DirArray As Variant

    'this is the array that handles the variations on the path, doesn't seem to be the problem
    DirArray = LM.Worksheets("Sheet2").Range("DirTable")

    Do While i <= UBound(DirArray)

       directory = DirArray(i, 1)

       dirname = "C:\blahblahblah"
       fileName = Dir(dirname & "*.xl??")
       pathname = dirname & fileName

       ChDir dirname
       ' Workbooks.Open (dirname & fileName)

       LM.Worksheets("Sheet1").Range("B" & i + 1 & ":G" & i + 1).Value = _
             Workbooks(filename).Worksheets("Sheet1").Range("B6:G6").Value

        i = i + 1

    '  Workbooks(fileName).Close

    Loop
End Sub

If I could just figure out what is different when they're in the same folder! Navigating with ChDir and ChDrive doesn't seem to do any good...

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
basaltanglia
  • 793
  • 1
  • 6
  • 12
  • In what way? By assigning an intermediate variable? Not sure how that helps... – basaltanglia May 27 '16 at 19:07
  • `Inter = Workbooks(pathname).Worksheets("Metrics").Range("B6:G6").Value` `LM.Worksheets("Sheet2").Range("B" & i + 1 & ":G" & i + 1).Value = Inter` Debugger flags the first line, same error message – basaltanglia May 27 '16 at 19:12
  • `Workbooks(filename)` when you do that, you need entire path I think. – findwindow May 27 '16 at 19:13
  • I've tried `Workbooks(pathname), Workbooks(dirname & filename), Workbooks(dirname & "\" & filename)` And other variants. And when I have msgbox or the debugger return the variables, they all look like I would expect... – basaltanglia May 27 '16 at 19:14
  • What about `Workbooks(pathname & "\" & filename)`? `debug.print` it to make sure it's correct. – findwindow May 27 '16 at 19:15
  • if you look above, pathname already contains filename. It would be redundant and inaccurate. – basaltanglia May 27 '16 at 19:16
  • Ah my fault. So maybe you just need `"\"`? Again, what does `debug.print` say? Is it correct?? – findwindow May 27 '16 at 19:16
  • Yeah, I just used pathname as the argument for both Workbooks() and debug.print, and it looks correct in the debug window but still throws the Subscript error. – basaltanglia May 27 '16 at 19:18
  • Then that book doesn't exist. Or something I don't know. – findwindow May 27 '16 at 19:19
  • Definitely exists. Definitely works when they're in the same folder, or I open the book first. Dammit. Thanks for trying, though. – basaltanglia May 27 '16 at 19:21
  • 2
    That syntax only works on *open* workbooks, regardless of whether they're in the same folder as the macro file or not... – Tim Williams May 27 '16 at 19:24
  • Ahahhahahaha Tim is correct... _as usual_. – findwindow May 27 '16 at 19:24
  • Yea, going with Tim's idea, I would create objects for the books you want to open. Slightly more coding but should be flawless. Edit: er, error free, not flawless XD – findwindow May 27 '16 at 19:25
  • Have you tried using SQL to get the range you require? Look at using ADO with Excel. – Nathan_Sav May 27 '16 at 19:26

1 Answers1

1

It's unclear exactly what you want to do, but this should be a working version of your posted code.

Is there only one Excel file per folder? Did you want to use directory in place of the hard-coded DIRNAME ?

Sub Import()

    Const DIRNAME As String = "C:\blahblahblah\"
    Dim directory As String, fileName As String, LM As Workbook, i As Integer
    Dim DirArray As Variant, wb As Workbook

    Set LM = Workbooks("LM.xlsm") 'ThisWorkbook ?
    DirArray = LM.Worksheets("Sheet2").Range("DirTable").Value

    For i = 1 To UBound(DirArray, 1)

        directory = DirArray(i, 1) 'what are these values ?

        fileName = Dir(DIRNAME & "*.xl??")

        If fileName <> "" Then

            'ChDir dirname '<< you do not need this if you pass the full path to Open...
            Set wb = Workbooks.Open(filename:=DIRNAME & fileName, _
                                    ReadOnly:=True, UpdateLinks:=0)

            LM.Worksheets("Sheet1").Range("B" & (i + 1) & ":G" & (i + 1)).Value = _
                       wb.Worksheets("Sheet1").Range("B6:G6").Value

            wb.Close False 'no save

        End If
     Next
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Ok, there's a possibility (whooooops) that I hadn't closed the target workbook the first time, so maybe I have had to open the file every time to use that syntax. So there's no way to grab the data without fully opening the file? Is there at least a way to suppress all dialogue options (like read-only & update links? I'm having trouble using those as optional arguments in Workbooks.Open) – basaltanglia May 27 '16 at 20:07