excel - Error "object variable or with block variable not set" when searching through code -


i'm trying write subroutine searches through range of cells, , returns column number of first cell specified value. have far:

dim startcol long dim rngsearch range dim rngfound range  usedcol = activeworkbook.sheets("...").usedrange.columns.count      set rngsearch = range(cells(6, 2), cells(6, usedcol))     set rngfound = rngsearch.find(what:="jun", lookin:=xlvalues, lookat:=xlwhole)     startcol = rngfound.column 

unfortunately gives me error "object variable or block variable not set". error must coming block variable not being set since i'm not using statements. i've used exact same line of code in other programs , has worked perfectly. i'm not sure i'm missing here. appreciate help, thanks.

also, when debug, line gets highlighted

startcol = rngfound.column 

let me know if need other information.

this happen if search unsuccessful, msdn:

range.find method (excel)

...

this method returns nothing if no match found.

link: https://msdn.microsoft.com/en-us/library/office/ff839746.aspx

you can test this:

if rngfound nothing     'code handle not found case else     startcol = rngfound.column end if 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -