vba - "Object variable or With block variable not set" error while adding data into ArrayList -
i'm new vba i'm not pretty sure i'm doing. please kind.
what i'm trying implement is, user select files file picker , put selected files' path arraylist. created filelist global variable initialize in workbook_open event.
i'm getting object variable or block variable not set error in adding string arraylist.
option explicit dim filelist object sub browse_click() dim fd filedialog dim ofd variant dim filepathname string set fd = application.filedialog(msofiledialogfilepicker) fd .buttonname = "select" .allowmultiselect = true .filters.add "csv files", "*.csv", 1 .title = "choose csv file" .initialview = msofiledialogviewdetails .show each ofd in .selecteditems filepathname = ofd filelist.add (filepathname) // <~~~~~~~~~~~~~~~~~ error here next ofd dim str object each str in filelist msgbox str next str end end sub private sub workbook_open() set filelist = createobject("system.collections.arraylist") end sub edit: want initialize arraylist once. user may click browse button several time , need keep files choose in arraylist.
with these changes code worked me:
option explicit dim filelist object sub browse_click() dim fd filedialog dim ofd variant dim filepathname string set filelist = createobject("system.collections.arraylist") 'initialization moved here set fd = application.filedialog(msofiledialogfilepicker) fd .buttonname = "select" .allowmultiselect = true .filters.add "csv files", "*.*", 1 .title = "choose csv file" .initialview = msofiledialogviewdetails .show each ofd in .selecteditems filepathname = ofd filelist.add (filepathname) next ofd dim str variant ' changed variant each str in filelist msgbox cstr(str) ' added conversion string next str end end sub
Comments
Post a Comment