+ Reply to Thread
Results 1 to 8 of 8

Division in VBA......not as expected....

Hybrid View

enggrahul78 Division in VBA......not as... 08-19-2019, 10:49 PM
jindon Re: Division in VBA......not... 08-19-2019, 10:59 PM
enggrahul78 Re: Division in VBA......not... 08-19-2019, 11:32 PM
jindon Re: Division in VBA......not... 08-19-2019, 11:45 PM
jolivanes Re: Division in VBA......not... 08-19-2019, 11:32 PM
Rick Rothstein Re: Division in VBA......not... 08-19-2019, 11:17 PM
Rick Rothstein Re: Division in VBA......not... 08-19-2019, 11:28 PM
enggrahul78 Re: Division in VBA......not... 08-19-2019, 11:32 PM
  1. #1
    Registered User
    Join Date
    07-19-2019
    Location
    USA
    MS-Off Ver
    office 365
    Posts
    29

    Division in VBA......not as expected....

    What is the best way to perform division with quotient and remainder in VBA ?

    I want to divide 7 by 3 and want to get Quotient as 2 and Remainder as 1..... I am reading a lot of articles and they talk about Banking rounding , integer rounding etc. All this is getting confusing..........What is the best way of accomplishing this ?

    1. For integer quotient i can use 7 \ 3 , but some articles says it might lead some unpredictable results.
    2. For remainder i can use MOD(7,3) which gives me 1 , but again some articles says it might lead some unpredictable results.
    3. Also when i write VBA code and manually do 7 \ 3 it works , but when i do it using EVALUATE method it will not work and gives error.
    String S = VAR1 \ VAR2 then EVALUATE(S) ---- gives error.
    String S = VAR1 / VAR2 then EVALUATE(S) ---- works fine.

    Any help witll be appreciated. My aim is to perform traditional mathematics and get the quotient and remainder..

    I did the below -

    Dim Var1 as Integer
    Dim Var2 as Intger
    Dim Var3 as Double

    Var 1 = 7 / 2 ====> it will give me 4 but i need it to give 3.
    Var 2 = 7 MOD 2 ===> it is correctly giving me 1.
    Attached Images Attached Images
    Last edited by enggrahul78; 08-19-2019 at 11:05 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Division in VBA......not as expected....

    String S = VAR1 \ VAR2 then EVALUATE(S) ---- gives error.
    Do you mean?
    Evaluate("int(" & Replace(S, "\", "/") & ")")

  3. #3
    Registered User
    Join Date
    07-19-2019
    Location
    USA
    MS-Off Ver
    office 365
    Posts
    29

    Re: Division in VBA......not as expected....

    Quote Originally Posted by jindon View Post
    Do you mean?
    Evaluate("int(" & Replace(S, "\", "/") & ")")
    I am creating various mathematical expressions at run time and evaluating those using the EVALUATE method. My expression in this case was "VAR1 \ VAR2" where Var1 = 7 and Var2 = 2.......... this gave me error.........

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Division in VBA......not as expected....

    Quote Originally Posted by enggrahul78 View Post
    . My expression in this case was "VAR1 \ VAR2" where Var1 = 7 and Var2 = 2.......... this gave me error.........
    Would be
    Evaluate("int(" & VAR1 & "/" & VAR2 & ")")

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,704

    Re: Division in VBA......not as expected....

    With 7 in Cell F1 and 2 in Cell G1:
    MsgBox "Quotient: " & Int([F1] / [G1]) & ", Remainder: " & [F1] Mod [G1]

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: Division in VBA......not as expected....

    For those who might be interested, here is (what I think is) an interesting note that I posted to the compiled VB newsgroups many, many, many years ago about Integer Division as it relates to the Banker’s Rounding issue…

    If floating point numbers are involved, integer division may not return the answer you expect. Most people think x\y is short-hand for Int(x/y), probably because of its name “Integer” division. However, VB applies Banker’s Rounding to the numerator AND denominator BEFORE performing the division. Many would think that this…

    MsgBox 4.5 \ 1.5

    should display 3 in the MessageBox, however, it prints out 2 instead. Banker’s Rounding rounds the 4.5 to 4 (the nearest even number) and rounds 1.5 to 2 (again, the nearest even number) AND ONLY THEN does it do the division and truncate the fractional part (4/2 producing 2 as the answer).

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: Division in VBA......not as expected....

    To follow up on my last post, some may find this interesting as well...

    Anything that requires rounding in VBA uses Banker’s Rounding (round 5s to the nearest previous even number). For example, both CLng(1.5) and CLng(2.5) will return 2 as the converted value because Banker’s Rounding lies at the heart of the conversion. HOWEVER, there is one (and only one as far as I have discovered) function in VBA that uses what I call “normal rounding”… you know, 5’s at the end of a number being rounded to the decimal position in front of the 5 always round up. That function is the Format function (along with some of its various incarnations, such as FormatNumber). For example, Format(2.5, "0") or Format(2.5, "#"), depending on if you want to force zeroes or not, will round up to 3 as expected (at least, as expected by me). Whatever number of decimal places you choose to round to (using the format pattern to set the number of decimal places). It appears that the Format function uses a Decimal subtype of a Variant as its data type since these expressions all round as expected for “normal rounding”…

    Format("1.234567890123456789012345674", "0.00000000000000000000000000")
    Format("1234.567890123456789012345675", "0.00000000000000000000000")

    Notice the limit of 28 digits (and, of course, the need to feed numbers with more than 15 digits as a String).

  8. #8
    Registered User
    Join Date
    07-19-2019
    Location
    USA
    MS-Off Ver
    office 365
    Posts
    29

    Re: Division in VBA......not as expected....

    So what logic shall i follow ??

+ 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. Expected table is not in the expected format - ADODB Connection to Read Only Excel file
    By Roshan10043 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2018, 10:54 AM
  2. IF= division
    By TrainGirl in forum Excel General
    Replies: 2
    Last Post: 10-01-2014, 04:51 PM
  3. division gives wrong answer (division)
    By Brice in forum Excel General
    Replies: 5
    Last Post: 12-24-2010, 10:13 PM
  4. Division by Zero
    By scotfitz in forum Excel General
    Replies: 5
    Last Post: 06-15-2008, 06:20 PM
  5. Division
    By David in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 07-26-2006, 06:05 AM
  6. Division
    By milkshake in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-27-2006, 12:12 AM
  7. Division help?
    By clayblay in forum Excel General
    Replies: 0
    Last Post: 01-12-2006, 08:00 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