+ Reply to Thread
Results 1 to 9 of 9

Specific VBA Calc.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2019
    Location
    london
    MS-Off Ver
    2007
    Posts
    50

    Specific VBA Calc.

    Hi Forum,

    Maybe a simple question but I still need some help with a formula clarification:

    Could somebody help me with this:
    TempCalc = CInt(TextBox3.Value) * (16*POWER(SQRT(CInt(TextBox1.Value)*(1-CInt(Textbox1.Value)))/(CInt(TextBox1.Value)*CInt(TextBox2.Value);2))
    what is wrong?

    Thanks in advance
    Last edited by alansidman; 07-09-2021 at 09:38 AM.

  2. #2
    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,995

    Re: Specific VBA Calc.

    TempCalc = 
    CInt(TextBox3.Value) * 
    (16*
    POWER(SQRT(CInt(TextBox1.Value)*
    (1-CInt(Textbox1.Value)))/
    (CInt(TextBox1.Value)*
    CInt(TextBox2.Value);
    2))
    Judging by the ;2 at the end, I suspect you are missing a Round function somewhere.
    Last edited by alansidman; 07-09-2021 at 09:38 AM.
    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


  3. #3
    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,995

    Re: Specific VBA Calc.

    Oh, and I think VBA would expect a comma rather than a semi-colon.

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Specific VBA Calc.

    TMS
    2, in my opinion, applies POWER.

    Kaizan
    You have used the sheet functions (POWER and SQRT) here as if they were VBA functions. But that's just one of the mistakes. Note that some VBA functions exclude the use of a sheet function in VBA-level calculations. This is the case with the VBA - Sqr function. You cannot use the sheet function SQRT in calculations. It is a bit different with the POWER function. You can use it in calculations, but you have to indicate that it is a sheet function (you'll see in a moment). Equivalent in VBA is sign (^), e.g.
     MsgBox Application.WorksheetFunction.Power(10, 2) & vbLf & _
             10 ^ 2 & String(2, vbLf) & _
             (Application.WorksheetFunction.Power(10, 2) = 10 ^ 2)
    However, the use of the SQRT sheet function will fail, e.g.
     MsgBox Application.WorksheetFunction.SQRT(100)
    you must use the VBA function
     MsgBox Sqr(100)
    With this knowledge, now try to write your calculations correctly.

    Artik
    Last edited by Artik; 07-09-2021 at 05:58 AM.

  5. #5
    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,995

    Re: Specific VBA Calc.

    @Artik: ah, thanks for that. POWER is not a function that I use so I couldn't see the relevance. Nor SQRT for that matter. Had worked out that it needed to be Application.WorksheetFunction.Power but was struggling with the bracketing.

    OK, this compiles:

    TempCalc = CInt(TextBox3.Value) * _
                16 * _
                Application.WorksheetFunction.Power( _
                    Sqr(CInt(TextBox1.Value) * _
                        (1 - CInt(TextBox1.Value)) / _
                        CInt(TextBox1.Value) * _
                        CInt(TextBox2.Value)), _
                    2)

  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,995

    Re: Specific VBA Calc.

    And this compiles:

    TempCalc = CInt(TextBox3.Value) * _
                16 * _
                (Sqr(CInt(TextBox1.Value) * _
                    (1 - CInt(TextBox1.Value)) / _
                    CInt(TextBox1.Value) * _
                    CInt(TextBox2.Value)) _
                ^ 2)

  7. #7
    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,995

    Re: Specific VBA Calc.

    Yes, but he asked for help and I wanted to see it corrected … even then, it might not be the calculation Kaizan wants
    Last edited by TMS; 07-09-2021 at 07:59 AM. Reason: Fix typo

  8. #8
    Registered User
    Join Date
    12-11-2019
    Location
    london
    MS-Off Ver
    2007
    Posts
    50

    Re: Specific VBA Calc.

    Hi Forum,

    Thanks a lot for all the help! I am, however, having issues with the actual calculation result, which should appear in TextBox5 i have attached multiple examples on how I think this could be done.

    Thanks again!
    Attached Files Attached Files

  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,995

    Re: Specific VBA Calc.

    I'd create a Calculation Command Button and put your code in there.

    Private Sub CommandButton1_Click()
    
    Dim TempCalc As Double
    
    Debug.Print "TextBox1: ", TextBox1.Value
    Debug.Print "TextBox2: ", TextBox2.Value
    Debug.Print "TextBox3: ", TextBox3.Value
    Debug.Print "TextBox4: ", TextBox4.Value
    
    Dim SqrtVal As Double
    SqrtVal = Sqr(CLng(TextBox1.Value) * 1 - CLng(TextBox1.Value))
    Debug.Print "SqrtVal: ", SqrtVal
    
    Dim DivVal As Double
    DivVal = (CLng(TextBox1.Value) * CLng(TextBox2.Value))
    Debug.Print "DivVal: ", DivVal
    
    Dim PowerVal As Double
    PowerVal = (SqrtVal / DivVal) ^ 2
    Debug.Print "PowerVal: ", PowerVal
    
    TempCalc = CLng(TextBox3.Value) * 16 * PowerVal
    Debug.Print "TempCalc: ", TempCalc
        
    TextBox5.Value = TempCalc
    
    End Sub
    But, as you can see, the results are zero.

    Results:
    TextBox1:     4
    TextBox2:     5
    TextBox3:     3
    TextBox4:     20
    SqrtVal:       0 
    DivVal:        20 
    PowerVal:      0 
    TempCalc:      0
    Unless my maths is off, this will give you a zero value:

    CLng(TextBox1.Value) * 1 - CLng(TextBox1.Value)
    You can't bracket it like this:
    CLng(TextBox1.Value) * (1 - CLng(TextBox1.Value))
    because the multiplication would give you a minus number and Sqr won't like that.

+ 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. [SOLVED] Need VBA that adjusts calc. field references in formula based on specific previous column
    By Betsy Simpkins in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2020, 02:16 AM
  2. Daily calc don't match monthly calc. why?
    By OldManSleepyhead in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-06-2019, 01:43 PM
  3. [SOLVED] Calc Numbers with decimals and having the Decimal be moved to new column to be Calc
    By TwistedFaith in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-24-2015, 12:01 AM
  4. Perform Calc On Specific Number Of Cells (Avoiding Blanks)
    By Phillycheese5 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-19-2013, 02:21 PM
  5. How do I set some wksht formulas to calc. manually and others to calc. automatically?
    By hoboking87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2013, 08:16 PM
  6. How do I create a macro(func) to calc. attendance with a set of specific conditions
    By Fredster31 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2010, 12:02 PM
  7. auto calc on, but have to edit (f2) cells to force re-calc..help!
    By Curt in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-13-2006, 02:10 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