+ Reply to Thread
Results 1 to 11 of 11

Input box to sum and display sum as msgbox

  1. #1
    Registered User
    Join Date
    02-06-2019
    Location
    Colorado
    MS-Off Ver
    2016
    Posts
    8

    Input box to sum and display sum as msgbox

    Hello I am struggling to make this assignment happen:Create a macro procedure that asks for three integers (one at a time) and displays their sum in a message box.
    Insert a module into the project, then insert a macro procedure named AddThreeNumbers. Code this procedure so that when it is executed, it will ask for three integers (one at a time), store them into three variables respectively, computes their sum, and display “The sum of [1st num], [2nd Num], and [3rd Num] is [the sum of the three numbers].” where each square bracket should be replaced by the numbers that the user put in. That is, if the user put in 10, 20, and 30 into the three boxes, it should display “The sum of 10, 20, and 30 is 60.” Make sure you declare and use appropriate variables.
    1. The message box should have the title, “My Own Adder”.
    2. Insert an ActiveX Command button displaying a picture of your choice, with the caption, “I Add Three Integers.”
    3. Link the AddThreeNumbers procedure to the button so that when the button is clicked, the procedure is executed.
    4. Save and submit the workbook.
    I currently have this code but my input boxes are not being saved as a variable.

    Please Login or Register  to view this content.
    Now I am getting an error message in my msgbox code when I add the Sum function. Addtionally, I still get the words "myValue..." in the message box not the numbers entered into the inputbox.
    Last edited by alexWhit21; 02-06-2019 at 07:07 PM. Reason: Updated code

  2. #2
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,482

    Re: Input box to sum and display sum as msgbox

    This reads a lot like a homework or learning task.. so I will help you along the way but wont give the solution.

    Remember your basic math, it still applies in VBA...
    & will not sum anything & will just combine 2 strings into 1.

    you will need to make an actual calculation to get the sum and then show that as a result in the messagebox
    the 3 variables that store the results of the inputboxes is a good start..
    Last edited by Roel Jongman; 02-06-2019 at 04:57 PM.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Input box to sum and display sum as msgbox

    Hello alexWhit21,

    I fully agree with Roel Jongman, and echo everything he says.

    Good luck.

    Thank you for adding Code Tags!
    Last edited by Winon; 02-06-2019 at 06:18 PM. Reason: Removed moderation
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Registered User
    Join Date
    02-06-2019
    Location
    Colorado
    MS-Off Ver
    2016
    Posts
    8

    Re: Input box to sum and display sum as msgbox

    Thanks for the help Roel. My variables do not to save the inputbox as a variable. My msgbox says what is in the quotes and does not pull the data stored. Can you help me understand what I am missing in my msgbox code?

  5. #5
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,482

    Re: Input box to sum and display sum as msgbox

    your variables work fine they are storing the values you put in the inputbox. Try using F8 to step thru your code you will see if you are past the first inputbox.
    what you are missing is the difference between a (numeric) value and a string. first you need numeric values to do calculations (look at your declaration of variables) and also the proper use of double quotes is an issue in your code.

    things to do
    1. add a calculation to get the sum of the variables
    2. make minor changes to your messagebox code to show the input and add something to show the sum in the msgbox too..

  6. #6
    Forum Contributor
    Join Date
    10-30-2014
    Location
    England
    MS-Off Ver
    2007 / 365
    Posts
    279

    Re: Input box to sum and display sum as msgbox

    I'll also agree with both of the above! So as for a little more help...

    Open your Locals window from the view menu and step through your code with F8. You'll see that your variables do populate as expected. So perhaps the issue may be in how you are referring to your variables. Also you should go to a spreadsheet, type ="A1" in a different cell and see what result that gives you, and is it what you'd expect; and if not, why might that be? Finally, a bit cryptic but, what makes a number a number?... why can we do 1+1 but not one+one?
    Last edited by EchoPassenger; 02-06-2019 at 06:47 PM.

  7. #7
    Registered User
    Join Date
    02-06-2019
    Location
    Colorado
    MS-Off Ver
    2016
    Posts
    8

    Re: Input box to sum and display sum as msgbox

    Roel, I think I'm really close but I get a "syntax error", any advice?

  8. #8
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,482

    Re: Input box to sum and display sum as msgbox

    you are really close.. but coding is brutal when it comes to spelling. even more so then in English class 1 mistake and the whole code will fail. But you still have several mistakes.
    still a bit of a blindspot with you for & and "
    you have to really cross all your t's and dot all the i'or in your case all the & and " in the right places
    on hint to keep going forward: only use double quotes around text strings never around variables or formulas.
    really important to get this right on your own. y'll never forget again

    I give you one more push in the right direction. Common functions used in a worksheet like sum dont work in vba, you will need to tell vba you want to use a function with the prefix "worksheetfunction."
    so make sure the sum function looks like this
    Please Login or Register  to view this content.
    and have another look at how you want to add that code to your message box.

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Input box to sum and display sum as msgbox

    Alex,

    What code doe you have so far ?

    Also use Application.Inputbox with which you can control the input.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  10. #10
    Registered User
    Join Date
    02-06-2019
    Location
    Colorado
    MS-Off Ver
    2016
    Posts
    8

    Re: Input box to sum and display sum as msgbox

    Please Login or Register  to view this content.
    This is what I have right now. I have tried to get the "" and & but I was just going to go into office house tomorrow to have him teach me becuase I realy can't figure it out.

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Input box to sum and display sum as msgbox

    OK, to summarize.
    Text goes between double quotes. Variables don't need to be double quoted. Text and variables are concatenated by the Ampersand eg &.
    If avalable I use the application version of a worksheetfunction because when running code with the Worksheetfunction and an error arises your code goes into Debug while
    using the application version your code runs further but you can trap the error using IsError.
    That being said here's something to study on.

    In a worksheet module.
    Please Login or Register  to view this content.
    In a standard module.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 02-07-2019 at 02:00 AM.

+ 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. Multiple MSGBOX with manual input
    By xatomicx in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2014, 06:44 PM
  2. Msgbox to input to last row problem with hidden rows
    By Donnerblue in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-12-2011, 01:48 AM
  3. Msgbox if you input a value smaller than another
    By tsioumiou in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 09-03-2010, 02:54 PM
  4. Date Code for input into MsgBox
    By Jessy01 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2009, 05:01 PM
  5. Pop MsgBox if cell input not a certain format
    By munkayboi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2007, 02:21 AM
  6. Input Title on MsgBox
    By pkeegs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2006, 06:20 PM
  7. [SOLVED] MsgBox Display
    By Michael S. in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2005, 06:05 PM

Tags for this Thread

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