+ Reply to Thread
Results 1 to 8 of 8

Display Range of Values with MsgBox

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Display Range of Values with MsgBox

    Hi,

    I am new to VBA Macros. I need help on how to display a range of cells in a Message Box.

    For example, to display A1:B10 on the same line in the message box separated by "has a balance of" in the form (A1 has a balance of B1, A2 has a balance of B2) etc.

    Thanks.

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Display Range of Values with MsgBox

    Hi.

    Try this code:

    Sub hasabalance()
        Dim rw As Long
        
        For rw = 1 To 10
            MsgBox Range("A" & rw) & " has a balance of " & Range("B" & rw)
        Next
    End Sub
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Display Range of Values with MsgBox

    Hi jraj1106,

    Thanks so much for your quick response.

    I was able to achieve this myself, I actually want the whole range to be displayed together at the same time instead of clicking OK and the next value coming up in the next message box and so on. I want to see the whole data from A1:B10 together.

    Thanks.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Try this.
    For Each cl In Range("A1:A10")
         strMsg = strMsg & cl.Value & " has a balance of " & cl.Offset(,1).Value & ","
    Next cl
    
    MsgBox Left(strMsg, Len(strMsg)-1)
    If posting code please use code tags, see here.

  5. #5
    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
    48,096

    Re: Display Range of Values with MsgBox

    To build on Norie's reply:

    Dim cl As Range
    Dim strMsg As String
    
    For Each cl In Range("A1:A10")
         strMsg = strMsg & cl.Value & " has a balance of " & _
         Application.WorksheetFunction.Text(cl.Offset(, 1).Value, "##0.00") & vbCr
    Next cl
    
    MsgBox Left(strMsg, Len(strMsg) - 1)

    Regards, TMS
    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


  6. #6
    Registered User
    Join Date
    02-21-2012
    Location
    Lagos
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Display Range of Values with MsgBox

    Thanks TMShucks it worked.

    Thanks Guys.

  7. #7
    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
    48,096

    Re: Display Range of Values with MsgBox

    You're welcome.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  8. #8
    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
    48,096

    Re: Display Range of Values with MsgBox

    Thanks for the rep.

+ 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