使用TextStream 对象
Option Explicit
Sub Export()
Const SHT_NAME = "Customer_Class_Clean-Up_Report"
Const RNG_NAME = "H7" ' cell
Const TABLENAME = "Table_Query_from_CHECKMATE"
Const COL = "Yard,AccountNum,CustomerCategory"
Const FOLDER = "C:\temp\"
Dim ws As Worksheet, rng As Range, cell As Range
Dim filename As String, n As Long
Dim FSO As Object, ts As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
' build export filename
Set ws = ThisWorkbook.Sheets(SHT_NAME)
filename = FOLDER & ws.Range(RNG_NAME).Value
If Len(filename) = 0 Then
MsgBox "Filename is blank", vbCritical
Exit Sub
End If
filename = filename & ".txt"
' create text file
Set ts = FSO.createTextfile(filename, True, True) 'overwrite, unicode
Set rng = ws.Range(TABLENAME & "[[#Headers],[" & COL & "]]")
For Each cell In ws.Range(rng, rng.End(xlDown))
ts.writeline cell
n = n + 1
Next
' finish
ts.Close
MsgBox n & " Rows exported from " & rng.Address & vbCrLf & _
" to " & filename, vbInformation, "Click OK to continue."
End Sub