Question

Ultimately I'm trying to write out a range into a text file. When I get the value of the range it returns a 2d variant array. However if I try to pass it to the converting function it gives me a type mismatch error.

The code is:

Dim Data As String
Set ts = fso.CreateTextFile("testfile.txt", True)
Data = ArrayToDelimitedString(wksMyWorkSheet.Range("rngMyRange").Value)
ts.Write (Data)
ts.Close

Public Function ArrayToDelimitedString(variantArray() As Variant) As String
   Dim delimitedString As String, index As Integer

   For index = 1 To UBound(variantArray(1))
      delimitedString = delimitedString & CStr(variantArray(1, index)) & ","
   Next
   ArrayToDelimitedString = Left(delimitedString, Len(delimitedString) - 1)
End Function

I'm wondering why the .Value returns a 2d array and why is it giving me this mismatch error.

Was it helpful?

Solution

The Range() method returns a Variant of Variant. In the case you want to use a 2D Array (Variant of Strings), use the Transpose method.

ArrayToDelimitedString(wApplication.WorksheetFunction.Transpose(wksMyWorkSheet.Range("rngMyRange").Value))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top