此代码可以读出指定行的数据到单元格中!Sub read_txt() Dim rr As String, n As Integer, xx As Integer xx = InputBox("请输入您读的行号!", "行号", 1) Open "c:\text.txt" For Input As #1 n = 1 Do While Not EOF(1) Input #1, rr If n = xx Then Sheets(1).Range("a1") = rr MsgBox "数据读取成功!" Input #1, rr End If n = n + 1 Loop If xx > n Then MsgBox "指定的文件只有" & n & "行,数据读取失败": GoTo g_exit g_exit: Close #1 End Sub
vba不支持数据类型 Dim rr ,n , xx ,FileNum xx = InputBox("请输入您读的行号!", "行号", 1) FileNum=FreeFile Open "c:\text.txt" For Input As #FileNum n = 1 Do While Not EOF(1) Input #FileNum, rr If n = xx Then Sheets(1).Range("a1") = rr MsgBox "数据读取成功!" Input #FileNum, rr End If n = n + 1 Loop If xx > n Then MsgBox "指定的文件只有" & n & "行,数据读取失败": GoTo g_exit g_exit: Close #FileNum稍微改了一下
Dim rr As String, n As Integer, xx As Integer
xx = InputBox("请输入您读的行号!", "行号", 1)
Open "c:\text.txt" For Input As #1
n = 1
Do While Not EOF(1)
Input #1, rr
If n = xx Then
Sheets(1).Range("a1") = rr
MsgBox "数据读取成功!"
Input #1, rr
End If
n = n + 1
Loop
If xx > n Then MsgBox "指定的文件只有" & n & "行,数据读取失败": GoTo g_exit
g_exit:
Close #1
End Sub
Dim rr ,n , xx ,FileNum
xx = InputBox("请输入您读的行号!", "行号", 1)
FileNum=FreeFile
Open "c:\text.txt" For Input As #FileNum
n = 1
Do While Not EOF(1)
Input #FileNum, rr
If n = xx Then
Sheets(1).Range("a1") = rr
MsgBox "数据读取成功!"
Input #FileNum, rr
End If
n = n + 1
Loop
If xx > n Then MsgBox "指定的文件只有" & n & "行,数据读取失败": GoTo g_exit
g_exit:
Close #FileNum稍微改了一下