excel vba - Find cell address in another workbook with its value: "Run time error '9' Subscript out of range" -


i'm working user form contains listbox , i'm trying list items in workbook cell addresses if found.

my code follow:

with me.listbox    i=0 listbox.listcount -1        colnum = worksheetfunction.match(listbox.list(i), workbooks("c:\sourcefile").worksheets(1).range("1:1"), 0)        msgbox "column :" & colnum     next end  

an error message pops telling me "run time error '9' subscript out of range".

i can see following problems might facing:

1) use application.match instead of worksheetfunction, latter not member of userform object

2) doubt name of listbox member not listbox, listbox1 or so

3)

workbooks("c:\sourcefile")

.. reference open workbook? if so, should using name (with extension) without path.

with me.listbox1    i=0 .listcount -1        colnum = application.match(.list(i), workbooks("sourcefile.xlsx").worksheets(1).range("1:1"), 0)        msgbox "column :" & colnum    next end  

finally, code can better optimized getting range search once all, not inside loop:

set r = workbooks("sourcefile.xlsx").worksheets(1).range("1:1") me.listbox1    i=0 .listcount -1        colnum = application.match(.list(i), r, 0)        msgbox "column :" & colnum    next end  

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -