2如何批量Excel文件转文本文件
工具/原料
- Microsoft Office Excel 2007
- Excel VBA
实例问题
- 1
文件夹里面有多个txt文件,好比有A.txt、B.txt、C.txt、D.txt......等等。此刻桌面上 有一个EXCEL文档,我但愿打开这个EXCEL文档后:当F1单位格字符输入为A,则主动导入这个指定路径下A.txt文本文档,当F1单位格字符输入为B,则主动导入这个指定路径下B.txt文本文档,其他文本文档照此类推....(按照F1单位格字符,主动导入对应的txt文档)
(百度知道问题)
处置方式/步骤
- 1
首先打开Microsoft Office Excel 2007,新建文档并保留文件名《Excel如何快速打开文本文件内容.xlsm》(演示文件,下面代码复制到能运行宏的工作簿都可以),并在A1输入“文件夹路径”,如下图。

- 2
鼠标移到当前工作表标签栏“Sheet1”表(需要在哪表计较在哪表),右键,弹出快捷菜单,如下图。

- 3
在快捷菜单找到【查看代码】并单击,打开VBE(宏)编纂界面,如下图。

- 4
在右边代码框中复制下面代码到该框中,如下图。
Private Sub Worksheet_Change(ByVal Target As Range)
'2020-7-20 22:11:39
Dim m As String, m1 As String
If Target.Row = 1 And Target.Column = 6 Then
m = Range("B1").Text
m1 = m &"\" & Target.Text &".txt"
Range("f2:f" & Rows.Count).ClearContents
On Error Resume Next
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & m1, Destination:=Range("$F$2"))
.Name = Target.Text &"_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
If Err.Number <> 0 Then
MsgBox"文件不存在!"
End If
End If
End Sub


- 5
以上操作动态过程如下:

- 6
回到“Sheet1”表窗口,在B1输入文本文件的文件路径,在F1输入文本文件名后,在F2显示该文件内容,操作如下图。



- 7
若是感觉这篇经验帮到了您,请点击下方的 “投票点赞" 或者“保藏”撑持我!还有疑问的话可以点击下方的 “我有疑问”,感谢啦!
注重事项
- 动态图片要双击当作零丁大图才能播放全过程。










