Results 1 to 7 of 7

Thread: VBA 7.1 Overflow Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    2

    Exclamation VBA 7.1 Overflow Error

    Hello,

    I keep getting an overflow error where I don't believe I should be getting an overflow error. I am a complete noob with VBA, but I do have a little coding experience with C++, so I'm familiar with integers exceeding the limit for the data type. Anyway, here's my code, which is still incomplete:

    Option Explicit

    Sub BookNetPay()
    Dim employeeName As String
    Dim grossPay As Currency
    Dim taxRate As Single
    Dim taxInDollars As Currency
    Dim netPay As Currency

    employeeName = InputBox("What is the employee's name?", "Input Employee Name")
    grossPay = InputBox("What is the employee's gross pay?", "Input Gross Pay")
    taxRate = InputBox("What is the tax rate? ", "Input Tax Rate") 'this is where I get the overflow error

    Range("A3").Value = employeeName
    Range("B3").Value = grossPay

    taxInDollars = taxRate * grossPay
    netPay = grossPay - taxInDollars

    End Sub

    My inputs are the following:

    employeeName = Homer Simpson
    grossPay = 10000
    taxRate = .2

    Full disclosure, I am using a Mac, which will make the haters hate.
    Any insight as to why I'm getting this overflow error would be much appreciated.

    Thank you.

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,665

    Re: VBA 7.1 Overflow Error

    I did not get any error using the code below to test.

    Code:
    Option Explicit
    
    
    Sub Test()
    
        Dim sngTest As Single
        
        sngTest = InputBox("What is the tax rate? ", "Input Tax Rate")
        Stop
    
    End Sub
    At the Stop, sngTest had a value of .2

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,203

    Re: VBA 7.1 Overflow Error

    InputBox returns a String, and you're assigning it directly to your variables which are currency and single, using the implicit typecast.
    before your problematic line, insert a temporary string-variable you assign the result of the InputBox, then do a Debug.Print CSng(tmpString)
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,934

    Re: VBA 7.1 Overflow Error

    i pasted your code into a new module, using your inputs, it ran without error, w10 64bit, excel 2013 32bit, so maybe it is a mac thing, i can not test
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,203

    Re: VBA 7.1 Overflow Error

    Just to get something out of the way: replace your single-variable with a currency one (or change them all to double)

    EDIT:
    Just noticed something: On which locale are you?
    I'm on german locale, and i had the following testroutine in Excel
    Code:
    Sub main()
    Dim sTemp As Single
    
        sTemp = InputBox("Test", "Test")
        Debug.Print sTemp
    
    End Sub
    As follows:
    entered "0,2" (german decimal separator is comma) --> output "0,2"
    entered ",2" --> Output "0,2" --> correct!
    entered "0.2" --> Output "2" -->!!!!! The dot is interpreted as the 1000-separator --> correct for german locale
    entered ".2" --> Type Mismatch RTE 13
    Last edited by Zvoni; Feb 13th, 2020 at 06:48 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    2

    Re: VBA 7.1 Overflow Error

    Thank you all for the quick replies. The code worked after I changed the variable type of taxRate to Currency. As a general rule, do all of the variable types have to be the same?

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,203

    Re: VBA 7.1 Overflow Error

    Quote Originally Posted by Travinator View Post
    Thank you all for the quick replies. The code worked after I changed the variable type of taxRate to Currency. As a general rule, do all of the variable types have to be the same?
    No, you are the Lord and Master of your variables.
    It's your job to handle them correctly.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

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



Click Here to Expand Forum to Full Width