从 Excel VBA/MACRO 中 TAB 分隔的 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2340949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:22:10  来源:igfitidea点击:

TAB separated txt file from Excel VBA/MACRO

excelvba

提问by andreas

I need to make an excel file that has a some values in it..into a tab separated values text file.... solution 1 -i can save it as a tab separated text file..but the issue is it also saves the column headers which i dont need.Dont know how to change that !

我需要制作一个包含一些值的 excel 文件......放入一个制表符分隔值文本文件......解决方案 1 - 我可以将它保存为制表符分隔的文本文件......但问题是它也保存了我不需要的列标题。不知道如何更改它!

solution 2--i have this code:

解决方案 2——我有这个代码:

Public Sub CharacterSV()
    Const DELIMITER As String = " |"
    Dim myRecord As Range
    Dim myField As Range
    Dim nFileNum As Long
    Dim sOut As String

    nFileNum = FreeFile
    Open "Test.txt" For Output As #nFileNum
    For Each myRecord In Range("A2:A" & _
                Range("A" & Rows.Count).End(xlUp).Row)
        With myRecord
            For Each myField In Range(.Cells, _
                    Cells(.Row, Columns.Count).End(xlToLeft))
                sOut = sOut & DELIMITER & myField.Text
            Next myField
            Print #nFileNum, Mid(sOut, 2)
            sOut = Empty
        End With
    Next myRecord
    Close #nFileNum
End Sub

Now i can control which cells to tab separate with the "Range" function in my code..however..how do i specify the tab delimiter"Const DELIMITER " instead of the pipe separator? Also..it seems to add the delimeter before the first value (ie from the first cell) how do i make it to start from the after the first value (but include the first cell value in the txt file?)

现在我可以用我的代码中的“范围”功能控制要使用制表符分隔的单元格..但是..如何 指定制表符分隔符“Const DELIMITER”而不是管道分隔符?另外..它似乎在第一个值之前(即从第一个单元格)添加分隔符我如何使它从第一个值之后开始(但在txt文件中包含第一个单元格值?)

回答by ghostdog74

how do i specify the tab delimiter "Const DELIMITER " instead of the pipe separator?

how do i specify the tab delimiter "Const DELIMITER " instead of the pipe separator?

for the above, you can use vbTab

对于上述,您可以使用 vbTab

回答by Stewbob

Also..it seems to add the delimeter before the first value (ie from the first cell) how do i make it to start from the after the first value (but include the first cell value in the txt file?)

另外..它似乎在第一个值之前(即从第一个单元格)添加分隔符我如何使它从第一个值之后开始(但在txt文件中包含第一个单元格值?)

This line of code:

这行代码:

sOut = sOut & DELIMITER & myfield.txt

adds the delimiter first, because the first time through your loop, sOut = "". Change this code to:

首先添加分隔符,因为第一次通过循环,sOut = ""。将此代码更改为:

sOut = sOut & myfield.txt & DELIMITER

Then after your loop has finished, if you don't want the extra delimiter on the end, use a trim operation to remove it.

然后在您的循环完成后,如果您不想在末尾添加额外的分隔符,请使用修剪操作将其删除。