“Object variable or With block variable not set” error while adding data into ArrayList

min9ox2

New Member
Joined
Aug 18, 2014
Messages
4
I'm new to VBA so I'm not pretty sure what I'm doing. Please be kind.
What I'm trying to implement is, user select the files from File Picker and put the selected files' path into ArrayList. I created fileList as global variable then initialize it in Workbook_Open event.
I'm getting <code>Object variable or With block variable not set</code> error in adding String into the ArrayList.

Code:
<code>Option Explicit  Dim fileList As Object  Sub Browse_Click()     Dim fd As FileDialog     Dim oFD As Variant     Dim Filepathname As String      Set fd = Application.FileDialog(msoFileDialogFilePicker)     With fd         .ButtonName = "Select"         .AllowMultiSelect = True         .Filters.Add "CSV Files", "*.csv", 1         .Title = "Choose CSV File"         .InitialView = msoFileDialogViewDetails         .Show          For Each oFD In .SelectedItems             Filepathname = oFD             fileList.Add (Filepathname) // <~~~~~~~~~~~~~~~~~ ERROR HERE         Next oFD          Dim Str As Object         For Each Str In fileList             MsgBox Str         Next Str     End With End Sub  Private Sub Workbook_Open()     Set fileList = CreateObject("System.Collections.ArrayList") End Sub</code></pre><code>
</code>
 
Last edited:
Could you repost code? Within [ Code ] Code here [ \Code] and have some new lines aswell?

Would make things easier!
 
Upvote 0
Could you repost code? Within [ Code ] Code here [ \Code] and have some new lines aswell?

Would make things easier!

Sorry bro. I've been trying to format the code several times. But no luck. After it saved, it happened like this. Now I'm getting this message.

  1. The administrator has specified that you can only edit messages for 10 minutes after you have posted. This limit has expired, so you must contact the administrator to make alterations on your message.

I can't even find the delete thread button. :(
 
Last edited:
Upvote 0
Sorry bro. I've been trying to format the code several times. But no luck. After it saved, it happened like this. Now I'm getting this message.

  1. The administrator has specified that you can only edit messages for 10 minutes after you have posted. This limit has expired, so you must contact the administrator to make alterations on your message.

I can't even find the delete thread button. :(

you copy it from excel VBA yes?

Just make a new post with the code inhere. Your code is now on one line :S

Just press the "#" while creating a post/reply, which will insert the code section for you.
 
Upvote 0
Here it is. Is it the correct one, and the one containing errors?

(Just opened it in notepad and pasted, then pressed [RETURN] (the keyboardbutton) for each new line, as that was the only thing needed.)

Code:
Option Explicit

Option Explicit
Dim fileList As Object
Sub Browse_Click()
Dim fd As FileDialog
Dim oFD As Variant
Dim Filepathname As String


    Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
                .ButtonName = "Select"
                .AllowMultiSelect = True
                .Filters.Add "CSV Files", "*.csv", 1
                .Title = "Choose CSV File"
                .InitialView = msoFileDialogViewDetails
                .Show
                


        For Each oFD In .SelectedItems
        
        Filepathname = oFD
            fileList.Add (Filepathname) '// <~~~~~~~~~~~~~~~~~ ERROR HERE
        Next oFD
        
        Dim Str As Object
               
        For Each Str In fileList
                MsgBox Str
        Next Str
End With
End Sub


Private Sub Workbook_Open()
Set fileList = CreateObject("System.Collections.ArrayList")
End Sub
 
Upvote 0
I'm not that sure about what you are looking for, but this works on my end


Code:
Dim fileList As Object
Sub Browse_Click()
Dim fd As FileDialog
Dim oFD As Variant
Dim Filepathname As String


    Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
                .ButtonName = "Select"
                .AllowMultiSelect = True
                .Filters.Add "CSV Files", "*.csv", 1
                .Title = "Choose CSV File"
                .InitialView = msoFileDialogViewDetails
                .Show
        
        Set fileList = CreateObject("System.Collections.ArrayList")


        For Each oFD In .SelectedItems
        
        Filepathname = oFD
            fileList.Add (Filepathname) '// <~~~~~~~~~~~~~~~~~ ERROR HERE
        Next oFD
        
       Dim counter As Integer
        Dim Item As Object
         counter1 = fileList.Count
        
        For i = 0 To counter1 - 1
                MsgBox fileList.Item(i)
        Next i
End With
End Sub

You might want to wait for a better answer though, but this stores the paths to the files you choose in the array fileList.Item(0 to x) 'with x as amount of files chosen.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top