Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting text in messagebox excel Vba

Tags:

excel

vba

I am new to Excel VBA and have written Vba code to loop through cells and get their values.After that do some processing and if it matches a certain criteria append them to a list with line break. Do this until all rows are done. This works perfectly fine and end result is as image below:

enter image description here

The thing is i want the formatting to look neat , so is there a way that the gap in between the text is same on all rows so it looks neat. The way i add the rows is :

          Dim tmpLine
          tmpLine = line & "      " & dateVal
          mainMessage = mainMessage & tmpLine & vbNewLine

Not sure if its the perfect way but that is what i know...

like image 439
confusedMind Avatar asked Aug 29 '13 23:08

confusedMind


2 Answers

I got this, maybe it'll solve your problem:

Sub msgBoxTest()

    MsgBox "inininegefedf" & vbTab & "1234" & vbCr & _
            "asdcainininegefedf" & vbTab & "1234" & vbCr & _
            "inininegefedf" & vbTab & "1234afsad", vbCritical

End Sub

Here is the result:

Result

like image 92
simpLE MAn Avatar answered Sep 24 '22 02:09

simpLE MAn


I've implemented a solution for this as complete as possible and spent a considerable effort for it. When transferring clsFmsgBox, frmFmsgBox, and modFmsgBox from FmsgBox.xlsm Workbook the below code leads to this kind of message box may be managed with comparably little effort. The Workbook above provides coding examples as well.

 With cFmsgBox
    .Title = "Message Box supporting formatted text. Example 1"
    .Msg = "This is the first " & _
     .b("Test ") & "message spanning over several lines and paragraphs. The default margins, spaces, and font size had been used. " & _
                   "No need to say that the form width had been adjusted manually in order to have an optimum appearance." & _
     .Lf & _
     .Lf & "The formats " & _
     .b("bold") & ", " & _
     .u("underline") & ", and " & _
     .i("italic ") & "and may be combined with any of the colours " & _
     .b(.i(.u(.red("red")))) & ", " & _
     .b(.i(.u(.blue("blue")))) & ", and " & _
     .b(.i(.u(.green("green")))) & "." & _
     .Lf & .Lf & _
           "5 different links may be included in the message text, either in the full form like " & _
     .link("www.google.com") & " or with a friendly name like " & .link("www.google.com", "google.com") & ", which masks the url behind it." & _
     .Lf & _
     .Lf & _
        "Also it shows 2 of the 6 possible reply buttons and that they may contain any text. Since the number of lines is maximized to 3 their width will be adjusted " & _
        "automatically - but will remain the same for all buttons. The string returned by the display call is identical with the string of the clicked reply button."

    .Reply1 = "Click this reply to continue with the next example"
    .Reply2 = "Click this reply to finish with the Message Box solution's features"
    .Dsply 318

    If .Reply = .Reply1 Then Example2
End With

The message is displayed in a dedicated UserForm with dynamically created labels for each formated text string and dynamically created (up to 6) command buttons. The key to the formatted message are the format properties .b("xxx") for bold, .i("xxx") for italic and so on, which can all be nested like .b(.i("xxx")) to get an italic,bold text for example.

Alternatively, instead of a concatenaed string the message text may be provided with RTF/HTML like formating tags whereby the open/close Tag character defaults to {} but may as well be changed to <>. As an example: "{b}bold{/b) {i}italic{/i}." will display: bold italic.

like image 24
Walter Rauschenberger Avatar answered Sep 25 '22 02:09

Walter Rauschenberger