![43060cceebd977046dd6e0240e01c4e2.png](https://img-blog.csdnimg.cn/img_convert/43060cceebd977046dd6e0240e01c4e2.png)
学习资源:《Excel VBA从入门到进阶》第52集 by兰色幻想
读取txt文件内容方法:
- input:从文件中读取指定数量的字符。
- Input #:把数据读出放在变量里,变量用逗号分隔
- Line Input #:取出完整的一行
搭配使用到的两个函数:
EOF(文件编号) 返回文本文件结尾。 LOF(文件编号) 判断文本文件的长度。
一、Input读取方式
Input 函数只用于以 Input 或 Binary 方式打开的文件,返回它所读出的所有字符,包括逗号、回车符、空白列、换行符、引号和前导空格等。
例1:读取a.txt,返回每个字符及其Asc值。
![1514a319c392b923515c3c5d81e738cf.png](https://img-blog.csdnimg.cn/img_convert/1514a319c392b923515c3c5d81e738cf.png)
Sub d1()
On Error Resume Next '出现错误跳转到下一步
Dim f, mychar
f = ThisWorkbook.path & "/a.txt"
Open f For Input As #1
Do While Not EOF(1) '循环至文件尾,EOF文件结尾
mychar = Input(3, #1) '读入一个字符
Debug.Print mychar & ":" & Asc(mychar)'显示到立即窗口。
Loop '按顺序读取,到下一个字符串,直至文件都读取完毕
Close #1
End Sub
![15c6e2024b36ac92586897aa06cf2693.png](https://img-blog.csdnimg.cn/img_convert/15c6e2024b36ac92586897aa06cf2693.png)
例2:把a.txt的内容一次性读取出来。
Sub d2()
Dim f, mychar, n, L
f = ThisWorkbook.path & "/a.txt"
n = FreeFile '所有文本内容
Open f For Input As n
L = LOF(n) 'LOF判断文本文件的长度(字节)
mychar = Input(L - 6, n) '要减去中文字符的个数(见下方注释)
Debug.Print mychar '显示到立即窗口。
Close #1
End Sub
注释:LOF判断文本文件的长度是字节数,并非字符个数。英文和数字都是一个字节,中文是两个字节,而inpt是输出字符个数,所以要用字节数将去中文字符个数才能得到总的字符个数。
![ae39bf65a385080cad73d7bed223eef7.png](https://img-blog.csdnimg.cn/img_convert/ae39bf65a385080cad73d7bed223eef7.png)
二、Input#读取方式
nput #读取
input 文件号(#…),变量1,变量2,..变量N
input #常用来读取write写的内容(分隔符为逗号,加双引号) 读非write文件会乱位。(write文件详见上篇笔记。
例3:用input #方式读取ruku.txt。
![4b508848d259f870c4a327afa83924ba.png](https://img-blog.csdnimg.cn/img_convert/4b508848d259f870c4a327afa83924ba.png)
Sub d3()
Dim f, x
f = ThisWorkbook.path & "ruku.txt"
Open f For Input As #1
Do While Not EOF(1) '读取文本直至文本结尾
Input #1, x
Debug.Print x
Loop
Close #1
End Sub
运行结果:
![b991ac8ae1c0cb2f7c00f2d8c62d2429.png](https://img-blog.csdnimg.cn/img_convert/b991ac8ae1c0cb2f7c00f2d8c62d2429.png)
可以看到入库日期不准确,数据位置也有些错位,那是因为ruku.txt不是write写入的文件。
那么我们换一个write写入的文件。
例4:用input #方式读取ruku2.txt。
![ce0a646b4a32aad7fa8a64bfac7ba957.png](https://img-blog.csdnimg.cn/img_convert/ce0a646b4a32aad7fa8a64bfac7ba957.png)
复习:write写入的文本,会自动生成逗号分隔数据,字符串被加双引号,日期会被两个#号包围。
Sub d4()
Dim f, y1, y2, y3, y4, y5
f = ThisWorkbook.path & "ruku2.txt"
Open f For Input As #1
Do While Not EOF(1) '循环到文本结尾
Input #1, y1, y2, y3, y4, y5 '一起获取同一行的五个数据,然后用空格分隔,在立即窗口输出
Debug.Print y1 & " " & y2 & " " & y3 & " " & y4 & " " & y5
Loop
Close #1
End Sub
运行结果:
![4c07951b1109a0d8f041f85bffee64d3.png](https://img-blog.csdnimg.cn/img_convert/4c07951b1109a0d8f041f85bffee64d3.png)
自动去除了多余的符号,只留下数据。
三、Line Input读取方式
Line Input 文件号,变量 取出完整的一行。
例5:用Line Input方式读取ruku3.txt。(write写入的文本)
![6b22a4bbe9e7f385d2128427e9422aab.png](https://img-blog.csdnimg.cn/img_convert/6b22a4bbe9e7f385d2128427e9422aab.png)
Sub d5()
Dim f, sr
f = ThisWorkbook.path & "Ruku3.txt"
Open f For Input As #1
Do While Not EOF(1)
Line Input #1, sr
Debug.Print sr
Loop
Close #1
End Sub
运行结果:
![1deff3253f5ee25c8aa71cb88a2780f4.png](https://img-blog.csdnimg.cn/img_convert/1deff3253f5ee25c8aa71cb88a2780f4.png)
将所有文本内容一字不差地全部读取,没有去除write写入的符号,所以Line Input不适合write写入的文本。
四、实例——拆分文本文件示例
例:把ruku3.txt按产品拆分读取数据到各自新的文本文件中。
![2182e24f259ac190a60874e017f639ef.png](https://img-blog.csdnimg.cn/img_convert/2182e24f259ac190a60874e017f639ef.png)
达到这样的效果:
![2182925109466ec9f1e922ce4706a3b7.png](https://img-blog.csdnimg.cn/img_convert/2182925109466ec9f1e922ce4706a3b7.png)
代码:
Sub 拆分()
Dim f, y1, y2, y3, y4, y5
Dim arr(1 To 5)
'存放标题行(每个文件都需要标题行,所以要单独设置数组存放)
Dim k
'判断行数
f = ThisWorkbook.path & "ruku3.txt"
Open f For Input As #1
Do While Not EOF(1)
Input #1, y1, y2, y3, y4, y5
k = k + 1
'如果是第一行,那就是标题行,只读取数据,不写入
If k = 1 Then
arr(1) = y1: arr(2) = y2: arr(3) = y3: arr(4) = y4: arr(5) = y5
Else
'根据产品名称建立新文件
Open ThisWorkbook.path & "拆分示例" & y2 & ".txt" For Append As #2
'如果文本的结尾为空,那就是新文件,需要先写入标题行,否则直接写入数据
If LOF(2) = 0 Then
Write #2, arr(1), arr(2), arr(3), arr(4), arr(5)
Write #2, y1, y2, y3, y4, y5
Else
Write #2, y1, y2, y3, y4, y5
End If
Close #2
End If
Loop
Close #1
End Sub