Rounding a number to the nearest 5 or 10 or X

前端 未结 13 1713
臣服心动
臣服心动 2020-11-28 08:11

Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number?

By 5:

 499 ->  500
2348 -> 2350         


        
13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 08:27

    something like that?

    'nearest
     n = 5
     'n = 10
    
     'value
     v = 496
     'v = 499 
     'v = 2348 
     'v = 7343
    
     'mod
     m = (v \ n) * n
    
     'diff between mod and the val
     i = v-m
    
    
     if i >= (n/2) then     
          msgbox m+n
     else
          msgbox m
     end if
    

提交回复
热议问题