vba - Infinite loop with .Find method -


i trying write vba script automate moving things around in spreadsheet has balance sheet imported accounting software. values on imported balance sheet start @ row 5, column has text describing values of each row mean, , columns b , d have amounts each item.

the subtotals each section , subsection of balance sheet on columns c , e. each subtotal has in cell formatted solid upper border.

i bring these subtotals same columns values (i.e, columns b , d). i've tried using .find method search cells specific format (cells upper border) , loop keep searching until find cells should have subtotal in it.

notes:

  1. i didn't use findnext because seems ignores format settings used in preceding find method, described here.
  2. i tried used findall function described tushar mehta go around problem findnext, didn't find cells specified format.

here's code. appreciated!

sub fixbalancesheet()   dim lookfor range   dim foundhere string 'address of cell should contain subtotal   dim beginat range, endat range, rng range 'set ranges sum subtotal   dim place string 'string address of cell contain subtotal   dim wheretolook range 'range subtotals found    'set workbook , worksheet   sheets("sheet1")     set wheretolook = range("a5:f100")     'every cell containing subtotal has upper border. so, cells containing border!     application.findformat.borders(xledgebottom)         .linestyle = xlcontinuous         .colorindex = xlautomatic         .tintandshade = 0     end     'call search using .find     set lookfor = wheretolook.find(what:="", after:=cells(5, 2), _         lookin:=xlformulas, lookat:=xlpart, _         searchorder:=xlbyrows, searchdirection:=xlnext, _         matchcase:=false, searchformat:=true)     if not lookfor nothing 'test if cell bottom border found       'what happens when subtotal cell found:       foundhere = lookfor.address       debug.print "found at: " & found       'loop set range, sum values , put them in right cell              '% find out range calculate subtotals , put value in right cells  %'         'call next search         application.findformat.borders(xledgebottom)         .linestyle = xlcontinuous         .colorindex = xlautomatic         .tintandshade = 0         end         set lookfor = wheretolook.find(what:="", after:=endat, searchformat:=true)         debug.print "lookfor is: " & lookfor.address         rem if lookfor.address = found ' not allow wrapped search           rem exit         rem end if       loop until lookfor nothing or lookfor.address = foundhere ' not allow wrapped search     end if   end end sub 

consider using range object loop through range. can add total if need grand total, easier way trying select cells have formatting.

for example:

sub teststackoverflowcode()     dim r range     dim rngtochk range      'this you'd insert wheretolook     set rngtochk = activesheet.range("b1:b4")      each r in rngtochk          'if top edge not not have border         if r.borders(xledgetop).linestyle <> xlnone             'copy cell value 2 cells right             r.offset(, 2).value = r.value         end if     next r  end sub 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -