导出为不带引号的文本

Export to Text without Quotation Marks

我的工作簿中有多个工作表。 每个工作表都有两列数据(ColA 和 ColC),我想将它们打印到单独的文本文件中。 附加代码生成两个文本文件:“WorksheetTab_LnFn.txt”和“WorksheetTab_FnLn.txt” 从我的 ColA 保存的文本文件没有引号,而从我的 ColC 保存的第二个文本文件有引号 - 我希望每个生成的文本文件都没有引号。

稍后我可能会有工作表,其中包含 ColA、ColC、ColE 和 ColG 中的数据,我想将每个数据 export/save/print 到一个文本文件——因此在那种情况下我想要四个单独的文本文档,所有没有引号。

我能找到的最好的代码是: and I have looked at How to create a text file using excel VBA without having double quotation marks?

我了解其中的大部分内容,但未能成功地将部分代码整合到我的代码中。理想情况下,我正在寻求减少代码和循环,以便它可以处理 ColA,然后处理 ColB,而无需两个单独的代码块。我确实使用了我找到的代码并进行了最小的更改,但不知道是否需要 Case LCase 行

        'Create FirstName LastName Isolation TXT files
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            For Each sh In Sheets
                Select Case LCase(sh.Name)
                    Case LCase("[COLOR=#0000ff]Master[/COLOR]"), LCase("[COLOR=#0000ff]Info[/COLOR]")
                    Case Else
                        sh.Range("A:A").Copy
                        Workbooks.Add
                        ActiveSheet.Paste
                        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & sh.Name & "_FnLn.txt", _
                            FileFormat:=xlTextMSDOS, CreateBackup:=False
                        ActiveWorkbook.Close False
                End Select
            Next

        'Create LastName FirstName Isolation TXT files
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            For Each sh In Sheets
                Select Case LCase(sh.Name)
                    Case LCase("[COLOR=#0000ff]Master[/COLOR]"), LCase("[COLOR=#0000ff]Info[/COLOR]")
                    Case Else
                        sh.Range("C:C").Copy
                        Workbooks.Add
                        ActiveSheet.Paste
                        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & sh.Name & "_LnFn.txt", _
                            FileFormat:=xlTextMSDOS, CreateBackup:=False
                        ActiveWorkbook.Close False
                End Select
            Next
    
            MsgBox "Text Files Created"
        End Sub

这应该可以满足您的要求:

Sub Tester()
    Dim filename As String, myrng As Range, sh As Worksheet, wb As Workbook
    Set wb = ThisWorkbook
    For Each sh In wb.Worksheets
        
        filename = wb.Path & "\" & sh.Name & "_FnLn.txt"
        Set myrng = sh.Range("C1:C" & sh.Cells(sh.Rows.Count, "C").End(xlUp).Row) 'use sh reference
        
        RangeToTextFile myrng, filename             'comma-separated
        'RangeToTextFile myrng, filename, vbtab     'e.g. for tab-separated file
    Next
    
    MsgBox "Text Files Created"
End Sub

'write a range `rng` to a text file at `fPath`.  Default separator is comma
Sub RangeToTextFile(rng As Range, fPath As String, Optional separator As String = ",")
    Dim data, r As Long, c As Long, sep, lo As String, ff As Integer
    
    ff = FreeFile()                     'safer than using hard-coded #1
    Open fPath For Output As #ff
    
    If rng.Cells.CountLarge = 1 Then
        ReDim data(1 To 1, 1 To 1)      'handle special case of single cell
        data(1, 1) = rng.Value
    Else
        data = rng.Value                'get all values as an array
    End If
    
    For r = 1 To UBound(data, 1)        'loop rows
        lo = ""                         'clear line output
        sep = ""                        'clear separator
        For c = 1 To UBound(data, 2)    'loop columns
            lo = lo & sep & data(r, c)  'build the line to be written
            sep = separator             'add separator after first value
        Next c
        Print #ff, lo                   'write the line
    Next r
    Close #ff
End Sub