- 读取文件
1.1.数据准备
1.2 代码准备
Sub test()
Dim buf As String, n As Long
Open "/Users/sixdog/Documents/VBA/test.txt" For Input As #1
Do Until EOF(1)
Line Input #1, buf
n = n + 1
Sheet1.Range("A" & n) = buf
Loop
Close #1
End Sub
1.3 执行结果确认
2.写入文件
2.1 准备空文件test2.txt
2.2 代码准备
Sub writeTest()
Open "/Users/sixdog/Documents/VBA/test2.txt" For Append As #1
For i = 1 To 10
Print #1, "nihao" & i
Next
Close #1
End Sub
2.3 结果确认
3.数据覆盖写入
3.1 数据准备
3.2 代码准备
Sub writeTest()
Open "/Users/sixdog/Documents/VBA/test4.txt" For Output As #1
For i = 1 To 10
Print #1, "你好" & i
Next
Close #1
End Sub
3.3 结果确认