+ Reply to Thread
Results 1 to 3 of 3

Return a single message box within a loop.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2011
    Posts
    1

    Return a single message box within a loop.

    Hi! I need to be make a multiplication table from a user input variable. I am using a for loop and have that part down; however, I need the multiplication table to be output in only one message box. This is the code I have so far....

    Sub Multiplication_table()
    Dim x As Integer
    Dim answer As Double
    
    x = InputBox("Please enter an integer to be used in a multiplication table.")
    
        For i = 1 To 10
        answer = x * i
        MsgBox ("" & x & " * " & i & " = " & answer)
        Next i
    End Sub
    Could someone please shed some light on how to stop from looping a new message box each time and only return one involving the entire table?
    For example I need to return
    1*2=2
    2*2=4
    3*2=6
    4*2=8
    5*2=10
    6*2=12
    ...and so on for x=2 THANKS
    Last edited by jeffreybrown; 10-19-2012 at 08:39 PM. Reason: Added code tags for new user...Please do so next time...Thanks.

  2. #2
    Registered User
    Join Date
    08-15-2012
    Location
    Las Vegas
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: Return a single message box within a loop.

    Try this code:

    Sub Multiplication_table()
    Dim x As Integer
    Dim answer As Double
    Dim asWs() As Variant
    Dim nWS As Integer
    
    x = InputBox("Please enter an integer to be used in a multiplication table.")
    
        For i = 1 To 10
              answer = x * i
     
        
        nWS = nWS + 1
        ReDim Preserve asWs(1 To nWS)
        asWs(nWS) = "" & x & " * " & i & " = " & answer
        
        Next i
        
    For i = LBound(asWs) To UBound(asWs)
        msg = msg & asWs(i) & vbNewLine
    Next i
    
    MsgBox "Multiplication Table for the number " & x & vbNewLine & msg
    
    End Sub
    I added each of your looping answers to an array and then displayed that array in the message box. See if that works.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Return a single message box within a loop.

    You can also accomplish it with just text manipulation without the use of arrays:

    Sub Multiplication_table()
      Dim i%, x%, answer$
        x = InputBox("Please enter an integer to be used in a multiplication table.")
          For i = 1 To 10: answer = answer & vbCrLf & "" & x & " * " & i & " = " & x * i: Next
      MsgBox "Multiplication Table for the number " & x & vbCrLf & answer
    End Sub
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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