vba - For loop doesn't run in worksheet -
i tried link common columns when click cell in policycomponents sheet, loop doesn't run - exits sub.
code snippet:
sub linkname() dim long dim shtusedrange, shtusedrangecol dim name string dim name1 string dim lookup_range range dim box shtusedrange = activesheet.usedrange.rows.count 'count used rows in activesheet shtusedrangecol = activesheet.usedrange.columns.count 'count used column in activesheet name = activecell.row 'row of selected cell name1 = activesheet.cells(name, 1).value 'name of row selected 'msgbox name1 set lookup_range = thisworkbook.sheets("policydetails").range("a1:z5000") 'set range of policy details search box = application.worksheetfunction.vlookup(name1, lookup_range, 1, false) 'to match name policy details msgbox box = 1 thisworkbook.sheets("policycomponents").rows.count step -1 if thisworkbook.sheets("policycomponents").cells(i, 1).value = box thisworkbook.sheets("policy viewer").cells(16, 2).value = thisworkbook.sheets("policycomponents").cells(i, 4).value end if next end sub
- you using name string type variable assigning row number value. means name
"2"
, not2
, cannot used when number required. never idea call variables same reserved words vba's.name
. - you using step -1 starting @ 1 means never go anywhere.
that should enough loop going.
sub linkname() dim long dim shtusedrange, shtusedrangecol dim rw long dim lu variant dim lookup_range range dim box variant 'count used rows in activesheet shtusedrange = activesheet.usedrange.rows.count 'count used column in activesheet shtusedrangecol = activesheet.usedrange.columns.count 'row of selected cell rw = activecell.row 'name of row selected lu = activesheet.cells(rw, 1).value 'msgbox lu 'set range of policy details search set lookup_range = thisworkbook.sheets("policydetails").range("a1:z5000") 'there no error control here if there no match 'to match name policy details box = application.worksheetfunction.vlookup(lu, lookup_range, 1, false) msgbox box = 1 thisworkbook.sheets("policycomponents").rows.count step 1 if thisworkbook.sheets("policycomponents").cells(i, 1).value = box thisworkbook.sheets("policy viewer").cells(16, 2) = _ thisworkbook.sheets("policycomponents").cells(i, 4).value 'probably best exit hte loop here unless want try , catch other matches 'exit end if next end sub
i renamed 2 of variables. didn't know nature of value (number/text/date) trying left variant.
Comments
Post a Comment