+ Reply to Thread
Results 1 to 5 of 5

New to VBA - Where's my mistake with this small bit of code?

  1. #1
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Minnesota
    MS-Off Ver
    Office 365
    Posts
    163

    New to VBA - Where's my mistake with this small bit of code?

    Good Day All

    I've only recently (a couple weeks) started to slowly teach myself VBA via an online step by step guide I've found, and now I'm trying to write my first bit of code from scratch. I'm trying to make an "IF" code, and it keeps giving me an the response to the "ELSE" part of the code, despite the condition being true. So where am I going wrong with this?

    Set up:

    Cell K5 = 15,000,000
    Cell K7 = 12,500,000
    Cell K8 = (K7/K5)-1

    Cell K8 Result = -16.7%

    Code:

    Please Login or Register  to view this content.
    Thanks for your help!
    Last edited by OpieWinston; 07-26-2017 at 10:25 AM.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: New to VBA - Where's my mistake with this small bit of code?

    Hi,

    You've declared change as an Integer, so it can only be a whole number, which will never be between your two decimal values. You should use Double instead.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Minnesota
    MS-Off Ver
    Office 365
    Posts
    163

    Re: New to VBA - Where's my mistake with this small bit of code?

    Holy cats! I figured it out right after posting, tried it, and it worked! Woot Woot! Came back here to amend the post and saw your reply.

    Thanks for the answer bud! I figured it out using Single instead of Double. I believe you that I should use double, but why double vs single when looking to round to the first decimal? I thought the big difference between single and double was how many decimal places you could have. Is there more to it then that?

    Thanks again for the help!

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: New to VBA - Where's my mistake with this small bit of code?

    Excel uses doubles internally, so a Single is an unnecessary conversion, but it won't make a difference at that level of precision.

  5. #5
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Minnesota
    MS-Off Ver
    Office 365
    Posts
    163

    Re: New to VBA - Where's my mistake with this small bit of code?

    So general rule is use doubles for everything then? Got it. Thanks thanks thanks! I'm sure I'll be back with more problems in the future as I try to find more reasons to write VBA! This is so awesome...

+ 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] Mistake in code
    By jj4jj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2016, 11:11 AM
  2. [SOLVED] Need one small code
    By soumyade5 in forum Excel General
    Replies: 3
    Last Post: 08-25-2015, 04:48 PM
  3. Small Changes to Code
    By painterartist in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-05-2014, 10:22 AM
  4. Can this small bit of code be combined to one line of code?
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-15-2011, 02:00 AM
  5. Where Is My Code Mistake For Listbox ?
    By blackvba in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-29-2008, 02:29 AM
  6. VB code almost works...but can't find a minor mistake
    By NadiaR via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2006, 01:35 PM
  7. Code mistake
    By Mirko Barkovic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-29-2005, 10:00 AM

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