Highlighted
Contributor

# VBA code needed: if a number, then apply a formula

So, I have this ridiculous formula I found online that takes a number (in cell A2, for example) and provides a number with 3 significant figures. It actually works well. Typically, I just use a neighboring column and apply it.

I presume that there's an easier way using VBA to simply take any number and send it though this formula and replace the number in the cell. Could you think of a way to do this?

something like, for every cell in the sheet,

- isnumber = TRUE then apply formula, replace the original number

- isnumber = FALSE then next (i.e. just leave the cell alone. In many cases, there will be text in cells)

Here is the sig fig formula:

``````=TEXT(TEXT(A2,"."&REPT("0",3)&"E+000"),
"0"&REPT(".",(3-(1+INT(LOG10(ABS(
1*TEXT(A2,"."&REPT("0",3)&"E+000")
)))))>0)& REPT("0",(3-(1+INT(LOG10(ABS(
1*TEXT(A2,"."&REPT("0",3)&"E+000")``````

Any ideas?

Thanks,

Greg

0 Replies