Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Excel文件转换为文本文件_Excel_Vba - Fatal编程技术网

将Excel文件转换为文本文件

将Excel文件转换为文本文件,excel,vba,Excel,Vba,我正在尝试将Excel文件转换为文本文件,每个列标题和行值都用“”包围,分隔符为 我发现了一个在一定程度上有效的代码。不起作用的是Excel中的最后一列没有值 在第2行,最后第二列的值为“1/1/2010” 我预计流量为“1/1/2010”;“” 我得到“2010年1月1日” 这是错误的,上传网站拒绝它 如何修复VBA代码 默认情况下,它保存在文档文件夹中。 如何将txt文件保存到特定的path\文件夹 公共子输出QuotedCSV() 常量QSTR为字符串“” 将我的记录变暗为范围 将my

我正在尝试将Excel文件转换为文本文件,每个列标题和行值都用“”包围,分隔符为

我发现了一个在一定程度上有效的代码。不起作用的是Excel中的最后一列没有值

在第2行,最后第二列的值为“1/1/2010”

  • 我预计流量为“1/1/2010”;“”
  • 我得到“2010年1月1日”
这是错误的,上传网站拒绝它

  • 如何修复VBA代码
  • 默认情况下,它保存在文档文件夹中。
    如何将txt文件保存到特定的path\文件夹
  • 公共子输出QuotedCSV()
    常量QSTR为字符串“”
    将我的记录变暗为范围
    将myField设置为范围
    暗绿色如长
    暗声如弦
    nFileNum=FreeFile
    打开“File2.txt”,输出为#nFileNum
    对于范围内的每个myRecord(“A1:A”和_
    范围(“A”&行数).End(xlUp.Row)
    以我的记录
    对于范围(.1)中的每个myField_
    单元格(.Row,256).End(xlToLeft))
    sOut=sOut&“;”&QSTR&_
    替换(myField.Text、QSTR、QSTR和QSTR)和QSTR
    下一个myField
    印刷品(南部,2)
    sOut=空
    以
    下一个我的记录
    关闭#nFileNum
    端接头
    
    当前代码只运行到每行占用单元格的末尾(因为它在每行上运行xlToLeft)

    您可以这样做-从标题行获取行长度,并将其用于每一行:

    Public Sub OutputQuotedCSV()
        Const QSTR As String = """"
        Const SEP As String = ";"
        Dim myRecord As Range
        Dim myField As Range
        Dim nFileNum As Long, rngOut As Range
        Dim sOut As String, s As String
        Dim lstRow As Long, lstCol As Long
    
        nFileNum = FreeFile
        Open "C:\Tester\File2.txt" For Output As #nFileNum
    
        'find the range to be exported
        With ActiveSheet
            lstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lstCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            Set rngOut = .Range(.Range("A1"), .Cells(lstRow, lstCol))
        End With
    
        'loop over each row in the export range
        For Each myRecord In rngOut.Rows
    
            sOut = ""
            s = "" 'no separator for first record
            'loop over each cell in the row
            For Each myField In myRecord.Cells
                'quote the cell text, escaping any embedded " with ""
                sOut = sOut & s & QSTR & Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
                s = SEP 'add the separator for subsequent fields
            Next myField
            Print #nFileNum, sOut
    
        Next myRecord
    
        Close #nFileNum
    End Sub
    

    当前代码只运行到每行被占用单元格的末尾(因为它在每行上运行xlToLeft)

    您可以这样做-从标题行获取行长度,并将其用于每一行:

    Public Sub OutputQuotedCSV()
        Const QSTR As String = """"
        Const SEP As String = ";"
        Dim myRecord As Range
        Dim myField As Range
        Dim nFileNum As Long, rngOut As Range
        Dim sOut As String, s As String
        Dim lstRow As Long, lstCol As Long
    
        nFileNum = FreeFile
        Open "C:\Tester\File2.txt" For Output As #nFileNum
    
        'find the range to be exported
        With ActiveSheet
            lstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lstCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            Set rngOut = .Range(.Range("A1"), .Cells(lstRow, lstCol))
        End With
    
        'loop over each row in the export range
        For Each myRecord In rngOut.Rows
    
            sOut = ""
            s = "" 'no separator for first record
            'loop over each cell in the row
            For Each myField In myRecord.Cells
                'quote the cell text, escaping any embedded " with ""
                sOut = sOut & s & QSTR & Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
                s = SEP 'add the separator for subsequent fields
            Next myField
            Print #nFileNum, sOut
    
        Next myRecord
    
        Close #nFileNum
    End Sub
    

    嗨,蒂姆,谢谢你的回答。我粘贴了你提供的代码。但它给了我一个记事本文件,上面只有这个-“”。。没有数据。我还有什么遗漏吗?嗨,蒂姆,谢谢你的回答。我粘贴了你提供的代码。但它给了我一个记事本文件,上面只有这个-“”。。没有数据。还有什么我遗漏的步骤吗?