L
Loomah
Hi all
This is driving me up the wall!
I've just discovered this JOIN function (ok, 5-6 years after it was
introduced but hey!)
I'm trying to get to grips with this in Excel by assigning a range of cells
to it. I'm not too hot on using arrays but this is another step out of my
reach.
I can create an array but VBA online help states that the array for the join
function should be one dimensional.
Below is the code I have been using - including all thoughts and debugging
and different attempts commented out.
How do I ensure I have one row of data in a 1D array to use with the Join
function?
'Create comma delimited list using JOIN function
Sub a()
Dim iLastCol As Integer
Dim vArrData() As Variant
Dim sRes As String
Dim i As Integer
iLastCol = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
'This initializes a 1D array ok
ReDim vArrData(1 To iLastCol)
'This generates array with 2 dimensions - why??
vArrData() = Range(Cells(1, 1), Cells(1, iLastCol)).Value
''This confuses me completely!! - see watch
'vArrData() = Array(Range(Cells(1, 1), Cells(1, iLastCol)).Value)
''This array works fine
'vArrData() = Array(2, 94, 100029, "Loom", "edge", #12/16/1965#)
'Test if array is created
Debug.Print "Array created : " & IsArray(vArrData)
'what's in the array(s)?
'2D array
On Error Resume Next
'I don't even understand how I got to the
'UBOUND element of this loop!
For i = LBound(vArrData) To UBound(vArrData, 2)
If Err.Number <> 0 Then
Debug.Print "2D", Err.Number, Err.Description
Exit For
End If
Debug.Print vArrData(1, i)
Next
On Error GoTo 0
'1D array
On Error Resume Next
For i = LBound(vArrData) To UBound(vArrData)
Debug.Print vArrData(i)
If Err.Number <> 0 Then
Debug.Print "1D", Err.Number, Err.Description
Exit For
End If
Next
sRes = Join(vArrData, ",")
Debug.Print "join", Err.Number, Err.Description
'5; Invalid procedure call or argument
MsgBox sRes
End Sub
TIA
Happy Friday
;-)
This is driving me up the wall!
I've just discovered this JOIN function (ok, 5-6 years after it was
introduced but hey!)
I'm trying to get to grips with this in Excel by assigning a range of cells
to it. I'm not too hot on using arrays but this is another step out of my
reach.
I can create an array but VBA online help states that the array for the join
function should be one dimensional.
Below is the code I have been using - including all thoughts and debugging
and different attempts commented out.
How do I ensure I have one row of data in a 1D array to use with the Join
function?
'Create comma delimited list using JOIN function
Sub a()
Dim iLastCol As Integer
Dim vArrData() As Variant
Dim sRes As String
Dim i As Integer
iLastCol = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
'This initializes a 1D array ok
ReDim vArrData(1 To iLastCol)
'This generates array with 2 dimensions - why??
vArrData() = Range(Cells(1, 1), Cells(1, iLastCol)).Value
''This confuses me completely!! - see watch
'vArrData() = Array(Range(Cells(1, 1), Cells(1, iLastCol)).Value)
''This array works fine
'vArrData() = Array(2, 94, 100029, "Loom", "edge", #12/16/1965#)
'Test if array is created
Debug.Print "Array created : " & IsArray(vArrData)
'what's in the array(s)?
'2D array
On Error Resume Next
'I don't even understand how I got to the
'UBOUND element of this loop!
For i = LBound(vArrData) To UBound(vArrData, 2)
If Err.Number <> 0 Then
Debug.Print "2D", Err.Number, Err.Description
Exit For
End If
Debug.Print vArrData(1, i)
Next
On Error GoTo 0
'1D array
On Error Resume Next
For i = LBound(vArrData) To UBound(vArrData)
Debug.Print vArrData(i)
If Err.Number <> 0 Then
Debug.Print "1D", Err.Number, Err.Description
Exit For
End If
Next
sRes = Join(vArrData, ",")
Debug.Print "join", Err.Number, Err.Description
'5; Invalid procedure call or argument
MsgBox sRes
End Sub
TIA
Happy Friday
;-)