Issues with 3 sig figs VBA code

mdeavila

New Member
Joined
Oct 13, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Thank you for your help in advance!

I have this macro to update significant figures that has been working great for some time, but recently we unlocked a spreadsheet to copy/paste data into a new program and we noticed that not all of the values are being actually truncated to the 3 sig figs we require. We were seeing this with values less than 1000 but I was able to correct this to values down to 100 and now I am stuck not knowing where else to look.

Any values less than 99.9 won't display correctly, I am attaching a spreadsheet as an example.

The weird part is that 3 sig figs are actually displayed, but when you click on the cell, the numbers are not actually updated to 3 sig figs (numbers below 100 that is). Nmbers highlited in yellow are basically incorrect, although display is correct, I hope this makes sense.

This is the code that has been used:



VBA Code:
Sub Format_Sig_Figs()

ActiveCell.Select

If IsNumeric(ActiveCell) Then

Select Case ActiveCell

Case Is = 0

Selection.NumberFormat = "0.00"

Case Is >= 99.95

Selection.NumberFormat = "0"

Case Is >= 9.995

Selection.NumberFormat = "0.0"

Case Is >= 0.9995

Selection.NumberFormat = "0.00"

Case Is >= 0.09995

Selection.NumberFormat = "0.000"

Case Is >= 0.009995

Selection.NumberFormat = "0.0000"

Case Is >= 0.0009995

Selection.NumberFormat = "0.00000"

Case Is >= 0.00009995

Selection.NumberFormat = "0.000000"

Case Is >= 0.000009995

Selection.NumberFormat = "0.0000000"

Case Is >= 0.0000009995

Selection.NumberFormat = "0.00000000"

Case Is >= 0.00000009995

Selection.NumberFormat = "0.000000000"



'Negative numbers

Case Is <= -1000

Selection.NumberFormat = "0"

Case Is <= -100

Selection.NumberFormat = "0"

Case Is <= -10

Selection.NumberFormat = "0.0"

Case Is <= -1

Selection.NumberFormat = "0.00"

Case Is <= -0.1

Selection.NumberFormat = "0.000"

Case Is <= -0.01

Selection.NumberFormat = "0.0000"

Case Is <= -0.001

Selection.NumberFormat = "0.00000"

Case Is <= -0.0001

Selection.NumberFormat = "0.000000"

Case Is <= -0.00001

Selection.NumberFormat = "0.0000000"

Case Is <= -0.000001

Selection.NumberFormat = "0.00000000"

Case Is <= -0.0000001

Selection.NumberFormat = "0.000000000"

End Select

Else: Selection.NumberFormat = "@"

End If



End Sub









Sub Round_Calc()



Dim sglOrigValue As Single

Dim sglDigits As Single

Dim sglRoundedValue As Single



If IsNumeric(ActiveCell) Then

Select Case ActiveCell

Case Is = "0"

Selection.NumberFormat = "0.00"

Case Is >= 1000

sglOrigValue = ActiveCell.Value

Range("IV1").Formula = "=INT(LOG(" & sglOrigValue & ")-2)"

sglDigits = Range("IV1").Value

Range("IV2") = "=ROUND(" & sglOrigValue & ",-" & sglDigits & ")"

sglRoundedValue = Range("IV2").Value

ActiveCell = sglRoundedValue

Case Is <= 1000

sglOrigValue = ActiveCell.Value

Range("IV1").Formula = "=INT(LOG(" & sglOrigValue & ")-2)"

sglDigits = Range("IV1").Value

Range("IV2") = "=ROUND(" & sglOrigValue & ",-" & sglDigits & ")"

sglRoundedValue = Range("IV2").Value

ActiveCell = sglRoundedValue

Case Is <= 100

sglOrigValue = ActiveCell.Value

Range("IV1").Formula = "=INT(LOG(" & sglOrigValue & ")-1)"

sglDigits = Range("IV1").Value

Range("IV2") = "=ROUND(" & sglOrigValue & ",-" & sglDigits & ")"

sglRoundedValue = Range("IV2").Value

ActiveCell = sglRoundedValue







End Select

End If

Range("IV1:IV2").ClearContents



End Sub





I basically added this portion and it seemed to have solved the issue with numbers less than 1000, but when adding the very last part (for numbers below 100) it does not seem like it made any differences



VBA Code:
Case Is <= 1000

sglOrigValue = ActiveCell.Value

Range("IV1").Formula = "=INT(LOG(" & sglOrigValue & ")-2)"

sglDigits = Range("IV1").Value

Range("IV2") = "=ROUND(" & sglOrigValue & ",-" & sglDigits & ")"

sglRoundedValue = Range("IV2").Value

ActiveCell = sglRoundedValue
 

Attachments

  • SigFigs_MD.PNG
    SigFigs_MD.PNG
    13.3 KB · Views: 9
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the Forum!

Try changing your variables declared as Single to Double.
 
Upvote 0
Hello,
Can't you just wrap your numbers with
VBA Code:
CDbl(Format(x,"0.00E000"))
or even without VBA as worksheet function
Excel Formula:
=--TEXT(x,"0.00E000"))
?
Regards,
Bernd
 
Upvote 0
Hello,
Can't you just wrap your numbers with
VBA Code:
CDbl(Format(x,"0.00E000"))
or even without VBA as worksheet function
Excel Formula:
=--TEXT(x,"0.00E000"))
?
Regards,
Bernd
The issues with something like this is that the data that I need to update varies a lot, the macro above works, except when copying/pasting data into a different program outside of excel. Somehow the display is correct, but once I click on the cell the displayed value is different.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top