+ Reply to Thread
Results 1 to 6 of 6

Adding first and last digits of a four digit number in vba

  1. #1
    Registered User
    Join Date
    02-13-2011
    Location
    OKLAHOMA
    MS-Off Ver
    Excel For Mac: 2011
    Posts
    3

    Adding first and last digits of a four digit number in vba

    I need to write a sub program that will add the first and last digits of a number. For example, for a number like 1234: I need the answer to be 5 (1+4).
    Your help is highly appreciated.
    Ben
    Last edited by CollegeKidd; 02-13-2011 at 06:40 PM.

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

    Re: Adding first and last digits of a four digit number in vba

    I guess this is your homework? What have you tried so far?

    regards
    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
    Registered User
    Join Date
    02-13-2011
    Location
    OKLAHOMA
    MS-Off Ver
    Excel For Mac: 2011
    Posts
    3

    Re: Adding first and last digits of a four digit number in vba

    I have no clue how to even separate the digits. In our lab we just covered the basics of writing a sub program. Didn't go over how to separate digits of anything. I have no clue on how to get started. Maybe define one variable as the first number, the second as the last then just add the variables. I just don't know how to separate them.
    Last edited by CollegeKidd; 02-13-2011 at 06:19 PM.

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

    Re: Adding first and last digits of a four digit number in vba

    This is Excel. Do you need a program or can you just use a formula?

    If you're using a formula, look at LEFT and RIGHT to getthe digits and SUM, or just a plus sign (+) to add them together.

    If you need a "sub program", you'd use the same approach. Define a variable, put 1234 in it, use LEFT to get the first digit and RIGHT to get the last digit. Add them together in another variable and do something with it ... maybe a Msgbox.

    Come back if you get stuck ... or to say thanks for pointing you in the right direction ;-)

    Regards

  5. #5
    Registered User
    Join Date
    02-13-2011
    Location
    OKLAHOMA
    MS-Off Ver
    Excel For Mac: 2011
    Posts
    3

    Re: Adding first and last digits of a four digit number in vba

    The left-right thing worked!!!! I didn't know how to separate them thanks. I just had it output it to cell and get the input from another cell. That is the way my assignment told me to do it.
    Ben

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

    Re: Adding first and last digits of a four digit number in vba

    You're welcome. Thanks for the feedback. It's good to "hear" the sense of achievement.

    Regards

+ 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