Contents of Array, MsgBox and Looping (1 Viewer)

wilderfan

Registered User.
Local time
Yesterday, 21:46
Joined
Mar 3, 2008
Messages
172
I'd like to use Msgbox to list the elements of an array, with each element listed below the previous one until all the elements appear within the MsgBox. I imagine I would need to use Chr(13).

Is it possible to use some sort of Do Loop within the coding for a MsgBox?

PS #1. The number of elements will vary.


PS #2. I do not want to have a separate MsgBox for each element. That would require the user to click OK multiple times.
 

boblarson

Smeghead
Local time
Yesterday, 21:46
Joined
Jan 12, 2001
Messages
32,059
There is a 255 character maximum for display on a message box. If you need more than that, use a form you build to look like a message box.

simply make it easy in code and use vbCrLf or vbNewLine instead.
 

wilderfan

Registered User.
Local time
Yesterday, 21:46
Joined
Mar 3, 2008
Messages
172
I don't think I'll breach the 255 limit.

This is the coding that I was trying so far:

================================================

'Define a variable to hold the number of elements in the array
Dim i As Integer
i = UBound(sNames) + 1
'State the number of items in the array in a MsgBox and list contents for user
Select Case i
Case 1
MsgBox "There is " & i & " element in the array:" & Chr(13) & sNames(0)

Case Is > 1
MsgBox "There are " & i & " elements in the array."

================================================

I stopped because I wasn't sure I could introduce looping into the coding for a MsgBox.
 

boblarson

Smeghead
Local time
Yesterday, 21:46
Joined
Jan 12, 2001
Messages
32,059
How about this:
Code:
Dim i As Integer
Dim strHold As String
Dim strMsg As String
 
For i = 0 To UBound(sNames)
    strHold = strHold & sNames(i) & vbCrLf
Next
 
Select Case i
   Case 1
      strMsg = "There is " & i +1 & " element in the array:" 
   Case Is > 1
 
      strMsg = "There are " & i + 1 & " elements in the array:"
   Case Else
      strMsg = "There are no elements in the array."
End Select
 
MsgBox strMsg & vbCrLf & strHold
 

wilderfan

Registered User.
Local time
Yesterday, 21:46
Joined
Mar 3, 2008
Messages
172
It looks good to me. I'll give it a try, Bob.

As usual, you manage to get to the end result several times faster than I could have.

Many thanks.
 

Users who are viewing this thread

Top Bottom