How to round to a certain number of significant figures using Excel VBA?

round vba
excel significant figures number format
excel significant digits of precision
vba truncate decimal places
vba significant digits
format vba as number
excel vba format number decimal places
vba round to nearest 1000

Edit: This part is solved!

So I have a bunch of calculations in a program I'm making, and the numbers are getting so long that I'm getting overflow errors. It's resulting from things like dividing a 50 digit number by another 50 digit number. I need a way to round specific numbers to a certain amount of significant digits.

Here's some more information. First of all, all of the numbers I will be rounding will always be less than 1. The numbers can get as small as to the 1E-50. I only care about the first 10 or so significant digits. I just need a way to round it off to around ten sig figs before moving on to the next calculation in order to prevent overflow errors. That, and having a number go out to 50 figures is very useless.

I was thinking of maybe looping through the number, digit by digit? The program could add 1 to a counter when the number is equal to zero, and then break out of the loop once it hits anything other than zero. Then the next step could truncate the number to that counter plus however many significant digits I want.

So for .000001234567812345678 for example. It would return 5 zeroes. If I wanted ten significant figures, I would do five + ten = 15. The program would then only keep the first 15 digits after the decimal, so the number would be truncated to .000001234567812.

Another idea might be using the log. Then the power the new number would be raised to would be the number of zeroes in front of the significant digits.

What I don't know is, first of all, how to go through a number digit by digit, and checking if each number is a zero. I also don't know how to truncate a number to a certain amount of digits.

Any help would be greatly appreciated!

Edit: This part is not solved.

I would feel bad making another post so soon, so I'm going to ask the question here. If it's not answered in 20 minutes or so, maybe I'll make another post. Why isn't this working?

Sub Rounding()
    Tracker = 0
    For i = 1 To 10
        Tracker = Tracker + 1
        Cells(1, Tracker) = Rnd
        Cells(1, Tracker) = Application.Evaluate("=Round(Cells(1, Tracker), 4 - (Int(Log(Cells(1,Tracker))) + 1))")
End Sub

I keep getting an error, #NAME?, in every single cell. This should round each number to four significant digits.

Assuming your value is in A2, try either of these:

=ROUND(A2, 3-(INT(LOG(A2))+1))



Taken from here.

How to round to a certain number of significant figures using Excel , . The value must be greater than or equal to 0 (>=0). So, the rest of the formula is just using the calculated exponent value to figure out the right number to give ROUND depending on the number of significant digits desired: = ROUND(1234567, - 6) // 1- (1+6) = -6 = ROUND(1234567, - 5) // 2- (1+6) = -5 = ROUND(1234567, - 4) // 3- (1+6) = -4 = ROUND(1234567, - 3) // 4- (1+6) = -3

Why would you want to loop when there is already a function which gives you what you want?

Sub Sample()
    Dim someNumber As Double

    '~~> .000001234567812345678
    someNumber = 1.23456781234568E-06

    '~~> This will give you .000001234567812
    Debug.Print Round(someNumber, 15)
End Sub

VBA Round, Rounding Significant Figures in Excel. Enjoy, It seems this doesn't work with some numbers. eg show 1 to 2sf should display 1.0, but shows 1  Note. This VBA function returns something commonly referred to as bankers rounding. So be careful before using this function. For more predictable results, use Worksheet Round functions in Excel VBA.

This will not work if 3-(Int(Log(Range("A2"))/log(10#))+1)) is negative (i.e. if there are more significant figures before the decimal point than you wish to round the number to). An alternative is

Public Function round_sigfig(ByVal myval As Double, ByVal fignum As Integer) As Double
factor = 10 ^ (fignum - (Int(Log(myval) / Log(10#))) - 1)
round_sigfig = Round(myval * factor, 0) / factor
End Function

VBA Round, RoundUp, and RoundDown Functions, The Microsoft Excel ROUND function returns a number rounded to a specified number of digits. The ROUND function is a built-in function in Excel that is  The ROUND function can be used in VBA code in Microsoft Excel. Here are some examples of what the ROUND function would return: Round (210.665, 2) Result: 210.66 'example of bankers rounding Round (210.67, 1) Result: 210.7 Round (210.67, 0) Result: 211 Round (210.67) Result: 211. For example:

I found out that in excel worksheet, one can use: =ROUND(A2, 3-(INT(LOG(A2))+1))

but in VBA, the code becomes: =Round(Range("A2"), 3-(Int(Log(Range("A2"))/log(10#))+1))

Significant Figures Function, If you need to round a number to a given (variable) number of specified digits or figures, you can do so with an elegant formula that uses the ROUND and LOG10​  The ROUND Function rounds a number to specified number of digits relative to the decimal. By using negative numbers we can round to the left of the decimal. As you can see, we a need a way to calculate the num_digits input in order to round to a specified number of digits.

MS Excel: How to use the ROUND Function (VBA), Tammy needs to round values in a worksheet to two significant digits. if there is a simple formula to round any given number to only two significant digits. You must have used Excel Round function to round the floating numbers up-to a specific number of decimal places. VBA Round function behaves totally in a different manner than the one you use in Excel. VBA Round function uses “round to even” logic. If the number you are trying to round has the last digit after decimal >= 0.6, VBA Round function rounds it up (Round UP). If the number you are trying to round have the last digit after decimal <= 0.4, it rounds it down (Round Down).

Excel formula: Round a number to n significant digits, She wonders if there is an Excel function or formula, she can use that For some people, finding the number of significant digits in a value  How do you tell Excel to round to a specific number of significant figures without having to use exponential notation? This Excel formula will do the trick: = ROUND (value, sigfigs-(1+ INT (LOG10 (ABS (value)))))

Rounding to Two Significant Digits (Microsoft Excel), I've come up with an Excel formula that will round a number to a user specified number of significant digits, as follows: =ROUND(number,-(INT(LOG(number)+1-​num_. I have limited VBA skills. Thanks! Replies continue  I have been working on a VBA user defined function to do Scientific Rounding with significant digits or a limited number of decimals and this is what I put together to use in a macro. To use on a worksheet like '=SciRound(B3,3)' press Alt + F11 to open the Visual Basic Editor and from the Insert Menu add a Module.

  • Worth pointing out that Excel can only work with 15 digits of precision: your 50-digit numbers cannot be represented "correctly". See Jerry's answer here:…
  • This works very well! So the number in red is how many significant figures I want. How would I actually use this in VBA? I tried Range("A4") = Application.Worksheetfunction.Round(A2,3-(Int(Log(A2))+1)) and it didn't work. I keep getting a "Invalid procedure call or argument".
  • Try Range("A2") instead of A2.
  • I changed it to Range("A4") = Application.Evaluate("=Round(A2,3-(Int(Log(A2))+1))") and it worked! It does round the last number, but I can just add one extra digit, and it shouldn't matter. Thank you!
  • Do you happen to know why the program I made using this isn't working? I put it in the above text, below my original question.
  • @TheTreeMan - Sorry, I was at lunch :) Siddharth Rout's answer looks correct.
  • It won't be the same number of zeroes every time. It can range from no zeroes to fifty zeroes. I could use the Round() function, but I'd need a way of detecting how many zeroes there are, so I know how many digits to round it to.
  • Using LittleBobbyTables's method, I got the program to work! However, after editing the program to move and apply to cells as they're filled, the program "broke". I put the code in question above, below the other question I had. Why does it keep giving me a #NAME? error? Is it because of the Cells() reference inside? It worked in a module I was testing out, for individual cells, until I changed it to move with the Tracker.
  • Change Cells(1, Tracker) = Application.Evaluate("=Round(Cells(1, Tracker), 4 - (Int(Log(Cells(1,Tracker))) + 1))") to Cells(1, Tracker) = Application.Evaluate("=Round(Cells(1," & Tracker & "), 4 - (Int(Log(Cells(1," & Tracker & "))) + 1))") Tracker is a variable and not a string ;)
  • Ahh, such a stupid mistake! Thank you. Edit: It's actually giving me the same error?
  • I put it into Excel, and changed it up a bit so it didn't rely on tracker anymore, just to see if it worked, and I got the #NAME? error in each cell. I put in =ROUND(Cells(1,1), 4 - (INT(LOG(Cells(1,1))) + 1)). If I click the error it says "This formula contains unrecognized text".