0

Ok guys sorry if the code tag doesn't work I haven't been on Stack Overflow in sometime. Anyways here is the issue I'm having, and I'm pretty sure I know what's going on but I could use some help. The code below I'm basically opening an excel workbook that will be storing file-paths to different workbooks on my computer, i'm placing them into an array. The array is storing the data properly by the debug message box i'm using, but when it gets to the line "Set tempBook = filePathArray(i, 1)" I get the errror "424 Object Required". Now I know for a fact that the item in the array I'm looking at in the Set tempBook line is looking at a filepath. My theory is that information stored in the array may have special formatting which isn't allowing the Set tempBook to recognize the string as a file-path. Sorry if this is really simple, this is my first time working with VBA and macro's I much prefer Java and C#. Anyway guys and girls any help woudld be greatly appreciated. Also for security I change the file path info for the filePathBook before posting.

To recap:

I'm retrieving file path information from a workbook on my desktop and storing it into an array, each cell in that workbook is holding a full filepath to the workbook

I'm then running a loop that will go through the array, 1 item at a time during the loop, and trying to pull each file path out individually, get some data, and then do it again with the next file path in the array.

The error I'm getting is when it gets down to trying to pull the first file path out of the array and place it into a variable that is set to be a Workbook i'm getting error "424 Object Required".

I have a debug message box in place so i know the location being looked at is housing the proper file path information, but I believe the array formatting may be causing a problem.

Any help to alleviate this issue would be greatly appreciated.

Sub get_data_from_file()

Dim actBook As Workbook         'active workbook
Dim filePathBook As Workbook    'filepath workbook

Dim pasteCounter As Integer     'paste counting variable in loop
Dim counter As Integer          'loop counter


'This sets the workbook the macro is in to be the active workbook to paste too
Set actBook = ActiveWorkbook

'Turn off screen update to speed up macro and make it not seem like the screen flashes
Application.ScreenUpdating = False

'set the filePathBook to point to the workbook storing the file paths for other books
Set filePathBook = Workbooks.Open("C:directory info\filePathBook")



Dim filePathArray As Variant    'declare array

'retrieve data from range cells and store in array, these are the file paths for other books
filePathArray = filePathBook.Sheets("Sheet1").Range("a1:a2").Value

'Save and close filePathBook
filePathBook.Save
filePathBook.Close

pasteCounter = 1    'initialize paste counter variable, it's used to move cell paste locations

'declare another workbook to use as the temporary variable in the loop to open and retrieve info from each workbook in the array
Dim tempBook As Workbook

'Looping structure to look at array and perform functions.
For i = 1 To UBound(filePathArray)


    MsgBox filePathArray(i, 1)                      'Debugging purposes: files are being stored properly


    Set tempBook = filePathArray(i, 1)              'get first workbook filepath and store in tempBook
    tempBook.Sheets("Sheet1").Range("a1:a4").Copy   'Copy cells a1:a4 from workbook

    actBook.Sheets("Sheet1").Activate               'Activate current book, this ensures it is always active in each run of the loop
    ActiveSheet.Cell(a, pasteCounter).Select        'Select proper cell to paste values down from

    Selection.PasteSpecial Paste:=xlPasteValues     'Paste Values

    pasteCounter = pasteCounter + 4     'increment paste counter to select cells below pasted cells each iteration

    'save and close tempBook
    tempBook.Save
    tempBook.Close
Next i

'Turn screen updating back on
Application.ScreenUpdating = True

End Sub

1 Answer 1

1
Set tempBook = filePathArray(i, 1)

here you're trying to assign a Variant/String to a Workbook object

Set tempBook = Workbooks.Open( filePathArray(i, 1) )

is likely what you need

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.