简体   繁体   English

自定义导出Excel到文本文件

[英]Custom Export Excel to text file

Is there a way, or a script, that will allow me to take a single column of data from an Excel file, and export it to a text file so that each field is separated by a comma and word space—in a single action. 有没有一种方法或脚本可以让我从Excel文件中提取一数据,然后将其导出到文本文件中,从而使每个字段在一个动作中被逗号和单词空间分隔。 Using the Save As csv in Excel still requires you to open the file in an editor and perform a search/replace to achieve this but that requires a second, and needless, step. 在Excel中使用“另存为csv”仍然需要您在编辑器中打开文件并执行搜索/替换以实现此目的,但这需要第二步,而且不需要做。

The result I'm looking for when exporting a COLUMN from Excel would look like this: 从Excel导出COLUMN时,我正在寻找的结果如下所示:

field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field, field 田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野,田野

Something like this perhaps? 大概是这样吗?

Supply your own filepath and filename. 提供您自己的文件路径和文件名。 Assumes data starts at row 2 in col B in Sheet1, but you can change these variables to suit in the code. 假定数据从Sheet1的col B的第2行开始,但是您可以更改这些变量以适合代码。

Sub WriteCSVFile()

Dim ws As Worksheet
Dim fName As String, Txt1 As String
Dim fRow As Long, lRow As Long, Rw As Long
Dim Col As Long

Set ws = Sheets("Sheet1")
fName = "C:\yourpath\yourfilename.csv"
fRow = 2
Col = 2
Txt1 = ""

    With ws
        lRow = .Cells(Rows.Count, Col).End(xlUp).Row

        Open fName For Output As #1

            For Rw = fRow To lRow
                Txt1 = .Range(.Cells(Rw, Col), .Cells(Rw, Col))
                    If Rw = lRow Then
                        Print #1, Txt1
                    Else
                        Print #1, Txt1 & ", ";
                    End If
            Next Rw

        Close #1

        MsgBox ".csv file exported"

    End With
End Sub

There is a tutorial here if you want further explanation. 如果您需要进一步的说明,可以在这里找到一个教程。

Sub WriteCSVFile() 子WriteCSVFile()

' Declare variables Dim ws As Worksheet Dim fName As String, Txt1 As String Dim fRow As Long, lRow As Long, Rw As Long Dim Col As Long '将变量Dim ws作为工作表Dim fName作为字符串,将Txt1作为String Dim将行作为长,将lRow作为长,将Rw作为长将Dim Col作为长

' Get active sheet Set ws = ActiveWorkbook.ActiveSheet '获取活动表集ws = ActiveWorkbook.ActiveSheet

' Ask user for output file fName = Application.GetOpenFilename() '询问用户输出文件fName = Application.GetOpenFilename()

' Stop processing if no file specified If fName = "False" Then Exit Sub '如果未指定文件,则停止处理如果fName =“ False”,则退出Sub

' Start row fRow = 1 '开始行fRow = 1

' Start column Col = 1 '开始列Col = 1

' Output temp Txt1 = "" '输出温度Txt1 =“”

With ws
    ' Get data from sheet
    lRow = .Cells(Rows.Count, Col).End(xlUp).Row

    ' Open output file
    Open fName For Output As #1

        ' Loop rows of data
        For Rw = fRow To lRow
            ' Store data in temp
            Txt1 = .Range(.Cells(Rw, Col), .Cells(Rw, Col))
                ' Check if end of dataset and write data to output accordingly 
                If Rw = lRow Then
                    Print #1, Txt1
                Else
                    Print #1, Txt1 & ", ";
                End If
        Next Rw

    ' Close output file
    Close #1

     ' Alert user of completion
    MsgBox ".csv file exported"

End With

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM