Consulting

Results 1 to 11 of 11

Thread: Create string for Messagebox

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location

    Create string for Messagebox

    I have the following code, what i need it to do is when it gets to the message box i want it to display in the box the value of the range a2:k5
    it goes through everthing ok untill it gets to the msgbox that is where i get the error.


    Option Explicit
    
    Sub sortorder()
    ' sortorder Macro
    Dim temp As range
    Set temp = ActiveSheet.range("a2:k5")
    range("B1:K201").Select
    Selection.AutoFilter
    Selection.Sort Key1:=range("K1"), Order1:=xlDescending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    MsgBox (temp)
    Selection.Sort Key1:=range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Selection.AutoFilter
    range("B2").Select
    End Sub


    Thank you if anyone can help

    Ian

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Do you want the Sum of the range?


    MsgBox Application.WorksheetFunction.Sum(temp)

  3. #3
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    I think it depends on what you mean by the value of the range. Do you want a total of the cells in the range or to list the contents of the range?

    TJ
    Oh dear, I need a beer

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    If you're looking for the sum of the range, as DRJ has shown, you can leave your msgbox the same and change your first two lines to ...

    Dim temp As Long
        temp = WorksheetFunction.Sum(ActiveSheet.Range("A2:K5"))
    But if you're just looking for it to go into a msgbox, DRJ's way is the simpler of the two and you can rid yourself of those 2 lines altogether. (Except your Dim, if you're using Option Explicit).

  5. #5
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    thanks drj,tinyjack and firefytr for the replys
    What i want is the list of contents of the range, so it would tell me the name and then in each column there would be points that i also need to be listed
    i only need it to list the top 4 results

    i have attached the file it relates to

    Thanks

    Ian

  6. #6
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    You need to build a string to display the list, something like:


    Dim strMsg as String
    Dim lngRow as Long
    Dim rngCell as Range
    'Do your sorting etc.
    For Each rngCell in temp
          If rngCell.Row <> lngRow Then
             strMsg = strMsg & Chr(10) & rngCell.Value
             lngRow = rngCell.Row
          Else
             strMsg = strMsg & " : " & rngCell.Value
          End If
       Next
    MsgBox strMsg
    HTH

    TJ
    Oh dear, I need a beer

  7. #7
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thank you for that TJ

    That works great, but there is a few things that i would like to change, when it displays the msgbox can i get it to put a number before the data ie: 1 for the first row then 2 then 3

    Thanks

    Ian

  8. #8
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    strMsg = strMsg & Chr(10) & rngCell.Value
    changed to

    strMsg = strMsg & Chr(10) & (rngCell.Row - 1) & " : " & rngCell.Value

    Should do the trick for you.

    TJ
    Oh dear, I need a beer

  9. #9
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thanks again TJ.
    That did the trick

    Ian

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Retitled
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Great code here..
    I benefit from them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •