+ Reply to Thread
Results 1 to 8 of 8

Divide Integer/Decimal inside one cell

  1. #1
    Registered User
    Join Date
    11-05-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    6

    Divide Integer/Decimal inside one cell

    I'm a complete VBE novice and can't seem to figure this one out.

    I have two percentages that I need to divide by each other that will be entered by the user into the range H10:M100. In order to cut down on entry time the user will enter the two percentages in the format ##.##, where the integer value is one percentage and the decimal value is the other.

    I then want a macro to split the two values, integer and decimal, and divide the percentage represented by the decimal value by the percentage represented by the integer value. I want the result to be displayed in the same cell that the information was entered into as an integer only percentage.

    For example, if 45.25 was entered into a cell in the H10:M100 range excel would perform the following actions:

    Split 45.25 into .45 and .25
    divide .25 by .45 = .5555555555
    report back in the same cell 56%

    45.45 would return 100% and so on.

    I've hit roadblocks every time I try to program it but I know its a simple action and somebody here can help me.

    Any help is appreciated. Thanks.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Divide Integer/Decimal inside one cell

    Please Login or Register  to view this content.
    Code goes in the sheet module:

    Adding Code to a Sheet module
    1. Copy the code from the post
    2. Right-click on the tab for the relevant sheet and select View Code. This opens the Visual Basic Editor (VBE) and shows the object module for the selected worksheet.
    3. Paste the code in the window
    4. Close the VBE to return to Excel
    Last edited by shg; 11-11-2009 at 03:11 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-05-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Divide Integer/Decimal inside one cell

    I appreciate the help. It's exactly what I need with one exception.

    In the case that the two involved percentages were 10% and 5% I would like to be able to enter them as "10.5" rather than "10.05". Or in the case where the two percentages were 75% and 150% they would be entered as "75.150".

    Your macro handles any two digit percentages perfectly but returns incorrect results for anything outside that (I probably misled you in the original post when I said numbers would be entered in "##.##" format). Can your code be tweaked to handle 1 digit and 3 digit percentages on either side of the decimal point as well? That would cover any number that would ever be entered into the affected cells for my use.

    Unfortunately even tweaking your programming to handle that is beyond my ability.

    Again, thanks for your help.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Divide Integer/Decimal inside one cell

    It would be easy if the separator character were something that made the cell non-numeric, like a comma, hyphen, ... in lieu of decimal. Otherwise, not so easy.
    Last edited by shg; 11-11-2009 at 06:25 PM.

  5. #5
    Registered User
    Join Date
    11-05-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Divide Integer/Decimal inside one cell

    The only reason why I used a decimal point is because it's easy access from the Numpad. I could use anything. What would the code look like if it used a comma?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Divide Integer/Decimal inside one cell

    Please Login or Register  to view this content.
    EDIT: You could change sSep to "+" if you want something on the numeric keypad.
    Last edited by shg; 11-12-2009 at 11:46 AM.

  7. #7
    Registered User
    Join Date
    11-05-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Divide Integer/Decimal inside one cell

    Hmm. No dice with this new program. No errors but it doesn't seem to do anything. Does it test properly for you?

    Thanks for the effort.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Divide Integer/Decimal inside one cell

    Tested A-OK for me. Two numbers separated by a comma.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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