Excel批量转txt
本文是对百度来的结果做一个简化,主要参考以下两篇百度经验
1、https://jingyan.baidu.com/article/ad310e80143a2d1849f49e08.html
2、https://jingyan.baidu.com/article/6181c3e0c8e6a1152ef1539a.html
所用示例数据为
(一)新建一个Excel文件,并在里面填写上如下内容
(二)ALT+F11打开VBA(宏)编辑界面,然后点菜单栏【插入】,下拉列表中点【模块(M)】
(三)插入了一个模块1,在代码框中复制如下代码:
1 Sub 列举文件名() 2 3 Dim m1$, m$, r% 4 5 m1 = Range("b1").Text 6 7 On Error Resume Next 8 9 Range("a4:c1000000").ClearContents 10 11 m = Dir(m1 & "\", vbReadOnly) 12 13 r = 3 14 15 Application.ScreenUpdating = False 16 17 While m <> "" 18 19 r = r + 1 20 21 Range("a" & r).Value = r - 3 22 23 Range("b" & r).Value = m 24 25 Range("c" & r).Value = FileDateTime(m1 & "\" & m) 26 27 m = Dir 28 29 Wend 30 31 Application.ScreenUpdating = True 32 33 MsgBox "完成" 34 35 End Sub
(四)回到新建的Excel,菜单栏中点【视图】,下列表中【宏】,再点击【查看宏(V)】打开宏对话框,选宏名“列举文件名 ”点【执行】,结果如图
(五)回到VBA(宏)编辑界面,在“列举文件名”代码下面复制下面代码
1 Sub 批量EXCEL文件存文本() 2 3 Dim fm1 As String, myt As Worksheet 4 5 Dim m$, m1$, m2$, m3$, i%, r%, n% 6 7 m = Range("b1").Text 8 9 r = Application.WorksheetFunction.CountA(Range("B4:B10000")) 10 11 fm1 = Range("B2").Text & "\" 12 13 For i = 1 To r 14 15 m1 = Range("b" & i + 3).Text 16 17 n = InStr(1, m1, ".", 1) 18 19 m2 = Left(m1, n - 1) 20 21 Workbooks.Open m & "\" & m1 22 23 For Each myt In Sheets 24 25 myt.Select 26 27 If IsEmpty(myt.UsedRange) Then 28 29 30 31 Else 32 33 m3 = myt.Name 34 35 ActiveWorkbook.SaveAs Filename:=fm1 & m2 & m3 & ".txt", FileFormat:= _ 36 xlUnicodeText, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _ 37 , CreateBackup:=False 38 39 End If 40 41 Next myt 42 43 ActiveWorkbook.Close SaveChanges:=False 44 45 Next i 46 47 End Sub
(六)回到新建的Excel,菜单栏中点【视图】,下列表中【宏】,再点击【查看宏(V)】打开宏对话框,选宏名“批量EXCEL文件存文本 ”点【执行】即可