Sub test()
Dim arr As String, brr
For Each brr In sheets("工作表1").Range("a1").CurrentRegion.Rows
arr = arr & Join(Application.Transpose(Application.Transpose(brr)), vbTab) & vbCrLf
Next
Open "C:\test.txt" For Output As #1
Print #1, arr
arr=""
Close #1
End Sub作者: 准提部林 時間: 2021-11-21 09:20
Sub test()
Dim arr As String, brr
For Each brr In Sheets("工作表1").Range("a1:z100").Rows
arr = arr & Join(Application.Transpose(Application.Transpose(brr)), vbTab) & vbCrLf
Next
Dim fso As Object, txt As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.CreateTextFile("c:\test.txt", True, Unicode:=True)
txt.WriteLine arr
txt.Close
arr = ""
Set fso = Nothing
Set txt = Nothing
請測試看看,謝謝
Sub test()
Dim Arr, Brr, Crr, FPath, sh%, i&, j%
FPath = ThisWorkbook.Path
For sh = 1 To Sheets.Count
FN = Sheets(sh).Name
Open FPath & "\" & FN & ".txt" For Output As #sh
Arr = Sheets(sh).Range("a1").CurrentRegion
ReDim Brr(1 To UBound(Arr, 2))
For i = 1 To UBound(Arr)
For j = 1 To UBound(Arr, 2)
Brr(j) = Arr(i, j)
Next
Crr = Join(Brr, " ")
Print #sh, Crr
Next
Close #sh
Next
End Sub作者: suchblue 時間: 2021-11-24 11:19
Sub test()
Dim arr As String, brr, txtutf8 As Object, txtutf8nobom As Object
For Each brr In Sheets("工作表1").Range("a1:e50").Rows '範圍
arr = arr & Join(Application.Transpose(Application.Transpose(brr)), vbTab) & vbCrLf
Next
'範圍不明用#2 .CurrentRegion.Rows, 或者改用#6 迴圈跑的Crr取代arr,比較不會抓錯範圍
Set txtutf8 = CreateObject("ADODB.Stream")
Set txtutf8nobom = CreateObject("ADODB.Stream")
With txtutf8 '有bom
.Charset = "UTF-8" '編碼
.Open
.WriteText arr
.Position = 0
.SaveToFile "c:\testutf8.txt", 2 '檔名
.Type = 2
.Position = 3
End With
With txtutf8nobom '沒bom
.Type = 1
.Open
txtutf8.copyto txtutf8nobom
.SaveToFile "c:\testutf8nobom.txt", 2
End With
txtutf8.close
txtutf8nobom.close
Set txtutf8 = Nothing
Set txtutf8nobom = Nothing