Sub DaoChu()Dim I As Integer, J As Long, RW As LongFor I = 1 To ActiveSheet.UsedRange.Columns.Count Open ThisWorkbook.Path & "\" & Cells(1, I) & ".txt" For Output As 1 For J = 2 To Cells(65536, I).End(3).Row Print #1, Cells(J, I).Value Next J Close 1Next IMsgBox "資料匯出完畢!", vbOKOnly, "匯出成功"End Sub
Excel每行資料匯出成txt.檔案的VBA程式碼:
步驟一 設定宏先把載入宏安全性設為中或低步驟二 插入模組alt+f11開啟VBA編輯器開啟 選單欄》插入》模組,把下面程式碼加進去
Sub DaoChu()Dim I As Integer, J As Long, RW As LongFor I = 1 To ActiveSheet.UsedRange.Columns.Count Open ThisWorkbook.Path & "\" & Cells(1, I) & ".txt" For Output As 1 For J = 2 To Cells(65536, I).End(3).Row Print #1, Cells(J, I).Value Next J Close 1Next IMsgBox "資料匯出完畢!", vbOKOnly, "匯出成功"End Sub
步驟三 儲存以後關閉EXCEL步驟四 測試再重新開啟excel,按ctrl+shift+P就完成匯出了,檔案在D盤根目錄下如果你想放在其他目錄,可以吧Open “D:\” &中的D:\改成你要的目錄,但是目錄不能有中文字元