将数据写入文本文件类型不匹配错误vba

时间:2018-01-11 02:04:06

标签: excel vba excel-vba

将变量变量vArr写入文本文件时出错。
在这一行我得到错误:

textData = CStr(vArr)

我搜索过但无法找到解决问题的方法。非常感谢你的帮助。

以下是完整代码:

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

1 个答案:

答案 0 :(得分:2)

使用Join将数组更改为字符串。

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

我已使用空格(即chr(32))作为分隔符,但您可以更改它。