在日常工作中,我们很多时候都会用到TXT,XML,JSON等文件作为数据储存介质,方便我们导进系统,数据库等;今天开号第一天先来一个Excel转TXT文件文件,VBA小源码!希望对你们工作有所帮助!!
Sub 导出数据() '导出数据
Dim wjm As String
Dim wjh As Integer
Dim hh As Long
Dim I As Long
Dim lh As Integer
Dim j As Integer
Dim txtwd As String
'弹窗选择保存路径,并注明保存类型
wjm = Application.GetSaveAsFilename(FileFilter:="文本文件(*.txt),*.txt", Title:="选择导出目录")
wjh = FreeFile 'TXT文件号
hh = [A100000].End(xlUp).Row '判断最后一行
lh = [xfd4].End(xlToLeft).Column '判断最后一列
Open wjm For Output As #wjh '打开目标文件
For I = 1 To hh '循环每一行
txtwd = "" '清空文档
For j = 1 To lh '循环每一列
txtwd = txtwd & Cells(I, j).Value & "~"
Next j '开始列循环J
Print #wjh, Left(txtwd, Len(txtwd) - 1) '逐行写入目标文件
Next I '开始行循环I
Close #wjh '关闭TXT
MsgBox "数据导出完成"
End Sub