excel vba - Run time error 91 in vba -
private sub commandbutton1_click() dim rng range dim range dim cell range dim hvalue double dim svalue double set rng = selection hvalue = 0 svalue = 0 each cell in rng next cell if cell.value > hvalue hvalue = cell.value 'here run time error 91 each cell in rng next cell if cell.value < hvalue , cell.value > svalue cell.value = svalue msgbox "hvalue= " & hvalue & "svalue=" & svalue end sub
your error because variable cell
valid locally inside loop , if statement outside loop. move if statements inside loop this.
private sub commandbutton1_click() dim rng range dim range dim cell range dim hvalue double dim svalue double set rng = selection hvalue = 0 svalue = 0 each cell in rng if cell.value > hvalue hvalue = cell.value next cell each cell in rng if cell.value < hvalue , cell.value > svalue cell.value = svalue next cell msgbox "hvalue= " & hvalue & "svalue=" & svalue end sub
Comments
Post a Comment