excel - No Object Variable Set when using multiple range.find and findnext() -
i trying search persons' name within range.find keep getting run-time error 91 - object variable or block variable not set. happens "rngfound" within "getpaid".
sub emailclick() dim lastseasonrow double lastseasonrow = worksheets("season 2014-2015").range("a" & worksheets("season 2014-2015").rows.count).end(xlup).row dim lastseasonemailrow1 double lastseasonemailrow1 = worksheets("email").range("a" & worksheets("email").rows.count).end(xlup).row dim rng range dim rngfound range dim getpaid range dim erroremail string dim colmycol new collection 'our collection j = 2 lastseasonrow set rng = worksheets("email").range("a2:a" & lastseasonemailrow1) set rngfound = rng.find(worksheets("season 2014-2015").cells(j, 1).value) if not rngfound nothing ' if found if doesitemexist(colmycol, rngfound.offset(0, 1).value) = false 'check if completed swimmer's family dim countswimmers string countswimmers = application.countif(worksheets("email").range("c2:c" & lastseasonemailrow1), rngfound.offset(0, 2).value) if countswimmers > 1 s = 1 countswimmers if s = 1 'if first swimmer set rng = worksheets("email").range("c2:c" & lastseasonemailrow1) set rngfound = rng.find(rngfound.offset(0, 2).value) debug.print rngfound.offset(0, -2).value set rngbfound = rngfound else 'next swimmer in family set rngfound = rng.findnext(rngfound) debug.print rngfound.offset(0, -2).value ********************** when debugging, above line highlighted. end if ********************************' todo: grab worksheet's name persons' name , money column** set getpaid = worksheets("season 2014-2015").range("a2:a" & lastseasonrow).find(rngfound.offset(0, -2).value) if not getpaid nothing 'if found if getpaid.offset(0, 14).value <> "" 'if owe money debug.print getpaid.offset(0, 14).value else end if end if next s 'write name list, if name in array skip it, when lastseasonrow, remove array. colmycol.add (rngfound.offset(0, -1).value) 'todo: change values below strings correspond aboves combined values if rngfound.value = "michael" call send_email_using_vba(rngfound.offset(0, 2).value, rngfound.offset(0, 1).value, rngfound.value, worksheets("season 2014-2015").cells(j, 15).value) end if else debug.print rngfound.value if worksheets("season 2014-2015").cells(j, 15).value <> "" 'if owe money if rngfound.offset(0, 3).value <> "" 'if multiple emails (primary , cc) if rngfound.value = "michael" call send_email_using_vba(rngfound.offset(0, 2).value, rngfound.offset(0, 1).value, rngfound.value, worksheets("season 2014-2015").cells(j, 15).value, rngfound.offset(0, 3).value) end if else if rngfound.value = "michael" call send_email_using_vba(rngfound.offset(0, 2).value, rngfound.offset(0, 1).value, rngfound.value, worksheets("season 2014-2015").cells(j, 15).value) end if end if end if end if end if else erroremail = erroremail + worksheets("season 2014-2015").cells(j, 1).value + vbnewline end if next j if erroremail <> "" msgbox ("no email found for: " & vbnewline & erroremail) end if end sub
thank you
edit: added images data reference:
email worksheet
season 2014-2015 worksheet
for proposed solution, need change getpaid variable type long , add variable (e.g. gotpaid) of type double.
dim getpaid long, gotpaid double
change following section of code.
set getpaid = worksheets("season 2014-2015").range("a2:a" & lastseasonrow).find(rngfound.offset(0, -2).value) if not getpaid nothing 'if found if getpaid.offset(0, 14).value <> "" 'if owe money debug.print getpaid.offset(0, 14).value else end if end if
to this.
worksheets("season 2014-2015") gotpaid = application.sumifs(.columns("o"), .columns("a"), rngfound.offset(0, -2).value) getpaid = application.countifs(.columns("a"), rngfound.offset(0, -2).value) if cbool(getpaid) 'if found if cbool(gotpaid) 'if owe money debug.print rngfound.offset(0, -2).value & ": " & gotpaid else end if end if end
by shuffling off second find operation, not redefining first , .findnext
should keep operating until meet countswimmers number. alternately not reusing same variables worksheet functions should work here.
due speciality helper functions doesitemexist not tested compile.
Comments
Post a Comment