Gabriel
Gabriel

Reputation: 142

vba macro display result of loop to msgbox

I creted a loop checking number of characters length with conditions but sadly it's not properly working, with approriate no. of loops but not reading the next line, I want to post the result in a MsgBox, but when I use the msgbox inside the loop I will get a msgbox for every result found or only one msgbox with one result.

What I would like is to display every result in 1 msgbox with a line vbNewLine after each result.

Below is my code:

    Public Sub Rs()

        Dim Text As String
        Dim NumChar As String
        Dim i As Integer
        Dim NumRows As Long

        Application.ScreenUpdating = False
        'Get Cell Value
        Text = Range("B2").Value
        'Get Char Length
        NumChar = Len(Text)
        NumRows = Range("B2", Range("B2").End(xlDown)).Rows.Count
        Range("B2").Select

        For i = 1 To NumRows
            'Character length validation
            If Len(Text) <= 15 Then
                    MsgBox Chr(149) & "     SVC_DESC " & Text & " has " & NumChar & " characters " & " and it's Valid !" & vbNewLine
                Else
                    MsgBox Chr(149) & "     SVC_DESC " & Text & " has " & NumChar & " characters " & " and Exceeded allowable number of characters!" & vbNewLine
            End If
          Next i
          Application.ScreenUpdating = True

    End Sub

Upvotes: 1

Views: 7509

Answers (2)

user4039065
user4039065

Reputation:

Build a string through concatenation and display the strings after exiting the loop.

Public Sub Rs()

    Dim Text As String
    Dim NumChar As String
    Dim i As Integer
    Dim NumRows As Long
    dim msg1 as string, msg2 as string

    Application.ScreenUpdating = False
    'Get Cell Value
    Text = Range("B2").Value
    'Get Char Length
    NumChar = Len(Text)
    NumRows = Range("B2", Range("B2").End(xlDown)).Rows.Count
    Range("B2").Select

    For i = 1 To NumRows
        'Character length validation
        If Len(Text) <= 15 Then
                msg1 = msg1 & Chr(149) & "     SVC_DESC " & Text & " has " & NumChar & " characters " & " and it's Valid !" & vbLF
            Else
                msg2 = msg2 & Chr(149) & "     SVC_DESC " & Text & " has " & NumChar & " characters " & " and Exceeded allowable number of characters!" & vbLF
        End If
      Next i
      Application.ScreenUpdating = True

      if cbool(len(msg1)) then
          msg1 = left(msg1, len(msg1)-1)
          MsgBox msg1
      end if
      if cbool(len(msg2)) then
          msg2 = left(msg2, len(msg2)-1)
          MsgBox msg2
      end if

End Sub

A MsgBox uses Chr(10) aka vbLF for new lines; vbNewLine is overkill.

Upvotes: 1

Vityata
Vityata

Reputation: 43585

Assign the new text to a string variable and display the string variable outside the loop:

Option Explicit

Sub TestMe()

    Dim i As Long
    Dim displayText As String

    For i = 1 To 3
        displayText = displayText & vbCrLf & i
    Next i

    MsgBox displayText

End Sub

enter image description here

Upvotes: 2

Related Questions