Nactrem
Nactrem

Reputation: 55

Writing data to text file type mismatch error vba

I'm getting error while writing variant variable vArr to text file.
In this line I'm getting error:

textData = CStr(vArr)

I have searched but couldn't find a solution to my problem. I'm much appreciated for your help.

Here is the full code:

Sub Perm()
    Dim rSets As Range, rOut As Range
    Dim vArr As Variant, lrow As Long

    Set rSets = Range("A1").CurrentRegion
    ReDim vArr(1 To rSets.Columns.Count)
    Set rOut = Cells(1, rSets.Columns.Count + 2)
    Perm1 rSets, vArr, rOut, 1, lrow
End Sub

Sub Perm1(rSets As Range, _
          ByVal vArr As Variant, _
          rOut As Range, _
          ByVal lSetN As Long, _
          lrow As Long)
    Dim fileName As String, textData As String
    Dim textRow As String, fileNo As Integer

    fileName = "E:\Projeler\test.txt"
    fileNo = FreeFile

    For j = 1 To rSets.Rows.Count
        If rSets(j, lSetN) = "" Then Exit Sub
        vArr(lSetN) = rSets(j, lSetN)
        If lSetN = rSets.Columns.Count Then
            lrow = lrow + 1
            textData = CStr(vArr)
            Open fileName For Output As #fileNo
            Print #fileNo, textData
            Close #fileNo
            'rOut(lrow).Resize(1, rSets.Columns.Count).Value = vArr
        Else
            Perm1 rSets, vArr, rOut, lSetN + 1, lrow
        End If
    Next j
End Sub

Upvotes: 2

Views: 592

Answers (1)

user4039065
user4039065

Reputation:

Change the array to a string with Join.

textData = CStr(join(vArr, chr(32)))

I've used a space (i.e. chr(32)) for the delimiter but you can change that.

Upvotes: 2

Related Questions