Option Explicit
Sub Test_ListOfVals()
'
' Demonstration of ListOfVals function
'
'
Dim N As Integer
Dim xlCell As Range
Dim xlVals(25) As Variant
'
' read in all cell values for the selection
'
N = 0
For Each xlCell In Selection
N = N + 1
If N > 25 Then
MsgBox "too much data for this demo (limited to 25 values)" & vbCrLf & _
"continuing with 25 values", vbCritical + vbOKOnly
N = 25
Exit For
End If
xlVals(N) = xlCell.Value
Next xlCell
'
' display values using ListOfVals with defaults
'
MsgBox "Demo of ListOfVals {using defaults}" & vbCrLf & vbCrLf & _
ListOfVals(xlVals, N)
'
' display values using ListOfVals with CrLf = 1 (CrLf after each item)
'
MsgBox "Demo of ListOfVals {CrLf = 1}" & vbCrLf & vbCrLf & _
ListOfVals(xlVals, N, , 1)
'
' display values using ListOfVals using CRLf = 1 and Index = 1 (show
' array index, array is "one-based")
'
MsgBox "Demo of ListOfVals {CrLf = 1, Index = 1}" & vbCrLf & vbCrLf & _
ListOfVals(xlVals, N, , 1, , 1)
'
' for floating point data, display values using ListOfVals and setting
' # dec pts at 3
'
If Selection.Column = 4 Then
MsgBox "Demo of ListOfVals {CrLf = 1, DecPt = 3, Index = 1}" & vbCrLf & vbCrLf & _
ListOfVals(xlVals, N, , 1, 3, 1)
End If
End Sub
Function ListOfVals(X, N, _
Optional Separ As String = " ", _
Optional CrLf As Integer = 0, _
Optional DecPt As Integer = 0, _
Optional Index As Integer = -1) As String
'
'****************************************************************************************
' Function: creates a text string from any array with each array value
' separated by a user-defined separator. This proc is quite useful
' when displaying array values via, say, MsgBox
' Passed Values:
' X [in, any] array of values
' N [in, integer] length of X
' Separ [in, string, OPTIONAL] separator to be used; default = " "
' CrLf [in, integer, OPTIONAL] CrLf flag:
' if = 0, only Separ separates array values {default}
' if = m, vbCrLF added every "m" values
' DecPt [in, integer, OPTIONAL} decimal formating value; default = 0 (do
' nothing)
' if = 0, nothing special is done
' if = k (k > 0), then the proc assumes X to be numeric and
' will format the data values with k places to the right of
' the decimal point
' Index [in, integer, OPTIONAL] flag to indicate if array index is to be
' included:
' if = -1, do not knclude
' if = 0, include as zero-based
' if = 1, include as one-based
'
'***************************************************************************************
'
'
Dim I As Long, CrLfCount As Long
Dim strFormat As String
If DecPt > 0 Then strFormat = "#." & String(DecPt, "#")
ListOfVals = ""
CrLfCount = 0
For I = 1 To N
Select Case Index
Case Is = -1
Case Is = 0
ListOfVals = ListOfVals & "[" & Trim(I - 1) & "] "
Case Is = 1
ListOfVals = ListOfVals & "[" & Trim(I) & "] "
End Select
If DecPt < 1 Then
ListOfVals = ListOfVals & X(I) & Separ
Else
ListOfVals = ListOfVals & Format(X(I), strFormat) & Separ
End If
CrLfCount = CrLfCount + 1
If CrLfCount = CrLf Then
ListOfVals = ListOfVals & vbCrLf
CrLfCount = 0
End If
Next I
End Function
|