+ Reply to Thread
Results 1 to 10 of 10

Formula sqrt to VBA conversion

Hybrid View

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Formula sqrt to VBA conversion

    Hi Guys

    In attached sample file I am trying to get F26 calculation...
    The code does not error but the answer is an error...Any ideas
    Option Explicit
    
    Sub Test()
    Dim CatSz As Long, Size As Long, Prop As Double, StdErr As Double
    Size = [F23]
    CatSz = [F24]
    Prop = CatSz / Size
    StdErr = Sqr((Prop * (1 - Prop) / Size))
    End Sub
    Attached Files Attached Files
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Formula sqrt to VBA conversion

    Hi,
    What do you mean by "answer is error"? Your code gave me the same result as manual calculations with formulas.
    What is the correct answer?

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Formula sqrt to VBA conversion

    Hi kasan

    This is my result ... Office 2007, 2013
    Untitled.png

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula sqrt to VBA conversion

    Quote Originally Posted by sintek View Post
    Hi kasan

    This is my result ... Office 2007, 2013
    Attachment 588614
    I also get the incorrect answer
    If someone has helped you then please add to their Reputation

  5. #5
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula sqrt to VBA conversion

    I broke the calc down into parts (adding the following)

    a = 1 - Prop
    b = a / Size
    c = Prop * b
    StdErr = Sqr(c)
    I Dimmed a,b and c as Double's

    The problem seems to occur in (b), it correctly calculates a = 0.35 but then gets 0.35 / 60 as being 5.833333333E-03

    ? strange

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,119

    Re: Formula sqrt to VBA conversion

    What makes you say it's an error output?

    Option Explicit
    
    Sub Test()
    Dim CatSz As Long, Size As Long, Prop As Double, StdErr As Double
    Size = [F23]
    CatSz = [F24]
    Prop = CatSz / Size
    StdErr = Sqr((Prop * (1 - Prop) / Size))
    
    Debug.Print "1", Size, CatSz, Prop, StdErr
    
    StdErr = Round(StdErr, 10)
    
    Debug.Print "2", Size, CatSz, Prop, StdErr
    
    End Sub
    1              60            39            0.65          6.15765106730372E-02 
    2              60            39            0.65          0.0615765107
    That's not an error, just a small number.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Formula sqrt to VBA conversion

    Hallo Trevor

    Tx for putting me out of my misery lol...Please elaborate ...
    just a small number.
    Edit... Now I get it ...Only noticed that all the calculations to follow after running Main code calculates as it should ...
    Small Magnitudes

    For numbers with the smallest possible magnitude (closest to 0), Double variables can hold numbers as small as -4.94065645841246544E-324 for negative values and 4.94065645841246544E-324 for positive values.
    Last edited by Sintek; 08-31-2018 at 07:49 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Formula sqrt to VBA conversion

    @ kasan...
    Your code gave me the same result as manual calculations with formulas.
    How did you achieve that ...

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,119

    Re: Formula sqrt to VBA conversion

    Well, to be exact, it's a small(ish) number with a lot of digits after the decimal point ... 6.15765106730372E-02.

    Excel is limited to the number of digits it can display/handle.

    If you round the result to, say, 15 digits after the decimal point, you get 0.061576510673037. That is,around six one hundredths (and a little bit)

    In the code above, I rounded the result to 10 digits. It rather depends on what level of accuracy you need.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,119

    Re: Formula sqrt to VBA conversion

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VB sqrt problem
    By Unipop in forum Excel General
    Replies: 4
    Last Post: 10-02-2016, 10:25 AM
  2. VB sqrt problem
    By Unipop in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-02-2016, 10:11 AM
  3. Problem with sqrt
    By Bartek120 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-11-2016, 06:11 PM
  4. =+g12*2.20452+10.518+(2*sqrt(10.9)-2*sqrt(g13))
    By luisfsantos in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-13-2013, 08:21 AM
  5. Excel 2007 : SQRT problems
    By frdranger25401 in forum Excel General
    Replies: 1
    Last Post: 04-14-2011, 01:56 PM
  6. UDF using SQRT function
    By Vlad999 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-04-2007, 09:14 PM
  7. how to multiply 29,5 with sqrt of 12
    By Smiley in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-10-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1