Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default pls help newbie with my significant figures function

Hello. I am having a little trouble with a simple little function I wrote
to handle significant figures in excel. I just created a VBA module with
this simeple code;

Function Sig(BaseNum, NumSigDig)

Sig = Round(BaseNum, NumSigDig - Len(Int(BaseNum)))

End Function

The intended functionality of this would be something like if R1C1=
42.0037 then =sig(a1,5) would return the number 42.004 (rounded to 5
significant digits).

Now this function mostly seems to work like a charm (there are some
limitations in this implementation but they are expected and not the
subject of this post)

What really has me stumped is the function seems to only work where
BaseNum (the number to rounded) is less than 10000 (so 9999.9999999 works
okay, but not 10000).

I am very new to VBA so I don't understand how data types are being
handled well enough to troubleshoot this problem. Any advice would be
greatly appreciated.

Thanks

James

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default pls help newbie with my significant figures function

James Bond wrote in
:

Hello. I am having a little trouble with a simple little function I
wrote to handle significant figures in excel. I just created a VBA
module with this simeple code;

Function Sig(BaseNum, NumSigDig)

Sig = Round(BaseNum, NumSigDig - Len(Int(BaseNum)))

End Function

The intended functionality of this would be something like if R1C1=
42.0037 then =sig(a1,5) would return the number 42.004 (rounded to 5
significant digits).

Now this function mostly seems to work like a charm (there are some
limitations in this implementation but they are expected and not the
subject of this post)

What really has me stumped is the function seems to only work where
BaseNum (the number to rounded) is less than 10000 (so 9999.9999999
works okay, but not 10000).

I am very new to VBA so I don't understand how data types are being
handled well enough to troubleshoot this problem. Any advice would be
greatly appreciated.

Thanks

James



Oh, I probably should have mentioned that when I say my function doesn't
work for values of 10000 or greater I mean that I get a #VALUE error in
the the spreadsheet.

Thanks again.
James

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default pls help newbie with my significant figures function

Have you tried this solution from Dana DeLouis?

Function SigDigits(n, d As Integer)
' = = = = = = = = = = = = = = = = = = = =
' Rounds a number(n) to (d) significant figures
' By: Dana DeLouis,
' = = = = = = = = = = = = = = = = = = = =
SigDigits = Val(Format(n, String(d, "#") & "E+00"))
End Function

--

Vasant




"James Bond" wrote in message
...
James Bond wrote in
:

Hello. I am having a little trouble with a simple little function I
wrote to handle significant figures in excel. I just created a VBA
module with this simeple code;

Function Sig(BaseNum, NumSigDig)

Sig = Round(BaseNum, NumSigDig - Len(Int(BaseNum)))

End Function

The intended functionality of this would be something like if R1C1=
42.0037 then =sig(a1,5) would return the number 42.004 (rounded to 5
significant digits).

Now this function mostly seems to work like a charm (there are some
limitations in this implementation but they are expected and not the
subject of this post)

What really has me stumped is the function seems to only work where
BaseNum (the number to rounded) is less than 10000 (so 9999.9999999
works okay, but not 10000).

I am very new to VBA so I don't understand how data types are being
handled well enough to troubleshoot this problem. Any advice would be
greatly appreciated.

Thanks

James



Oh, I probably should have mentioned that when I say my function doesn't
work for values of 10000 or greater I mean that I get a #VALUE error in
the the spreadsheet.

Thanks again.
James



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default pls help newbie with my significant figures function

On Sun, 29 Aug 2004 18:08:25 GMT, James Bond wrote:

Hello. I am having a little trouble with a simple little function I wrote
to handle significant figures in excel. I just created a VBA module with
this simeple code;

Function Sig(BaseNum, NumSigDig)

Sig = Round(BaseNum, NumSigDig - Len(Int(BaseNum)))

End Function

The intended functionality of this would be something like if R1C1=
42.0037 then =sig(a1,5) would return the number 42.004 (rounded to 5
significant digits).

Now this function mostly seems to work like a charm (there are some
limitations in this implementation but they are expected and not the
subject of this post)

What really has me stumped is the function seems to only work where
BaseNum (the number to rounded) is less than 10000 (so 9999.9999999 works
okay, but not 10000).

I am very new to VBA so I don't understand how data types are being
handled well enough to troubleshoot this problem. Any advice would be
greatly appreciated.

Thanks

James


Here are some routines to round to significant digits:


VBA
==============================
Function RoundSigDigits(N As Double, SigDigits As Integer) As Double
RoundSigDigits = Application.WorksheetFunction.Round _
(N, Fix(-Log(Abs(N)) / Log(10)) + SigDigits + (Abs(N) 1))
End Function
========================

Various worksheet formulas:

=ROUND(F1,TRUNC(-LOG(ABS(F1)))+SigDigits-(ABS(F1)1))

=ROUND(A1,SigDigits-1-INT(LOG10(ABS(A1)))) (Walkenbach)

=--TEXT(A1,"."&REPT("0",SigDigits)&"E+000") (Harlan)

============================
--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default pls help newbie with my significant figures function

On Sun, 29 Aug 2004 18:08:25 GMT, James Bond wrote:

Hello. I am having a little trouble with a simple little function I wrote
to handle significant figures in excel. I just created a VBA module with
this simeple code;

Function Sig(BaseNum, NumSigDig)

Sig = Round(BaseNum, NumSigDig - Len(Int(BaseNum)))

End Function

The intended functionality of this would be something like if R1C1=
42.0037 then =sig(a1,5) would return the number 42.004 (rounded to 5
significant digits).

Now this function mostly seems to work like a charm (there are some
limitations in this implementation but they are expected and not the
subject of this post)

What really has me stumped is the function seems to only work where
BaseNum (the number to rounded) is less than 10000 (so 9999.9999999 works
okay, but not 10000).

I am very new to VBA so I don't understand how data types are being
handled well enough to troubleshoot this problem. Any advice would be
greatly appreciated.

Thanks

James


I forgot to answer your question about why your routine didn't work.

In recent versions of VBA, the VBA Round function is different from the Excel
worksheet function. In the VBA Round function, I do not believe that
numdecimalplaces can be negative.

If you prefer your routine to the one's I provided, you could either restrict
numdecimalplaces to being not less than zero, or use worksheetfunction.round


--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default pls help newbie with my significant figures function

Ron Rosenfeld wrote in
:


I forgot to answer your question about why your routine didn't work.

In recent versions of VBA, the VBA Round function is different from
the Excel worksheet function. In the VBA Round function, I do not
believe that numdecimalplaces can be negative.

If you prefer your routine to the one's I provided, you could either
restrict numdecimalplaces to being not less than zero, or use
worksheetfunction.round


--ron


Ahhh, that would certainly explain the problems I was having. Thank you
very much for that explanation as well as the info on other ways to
achive my goals.

James

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i show 3 significant figures peter Excel Discussion (Misc queries) 1 February 18th 10 07:51 PM
significant figures Crystal Excel Worksheet Functions 14 July 8th 09 07:36 PM
significant figures? bgarey Excel Discussion (Misc queries) 15 September 3rd 08 08:01 PM
Rounding/Significant figures cloots Excel Worksheet Functions 5 September 1st 05 04:03 PM
Significant figures (not decimal places) Gene Solomon Excel Worksheet Functions 2 December 9th 04 09:42 PM


All times are GMT +1. The time now is 11:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"