VBA线路输入与输入

时间:2013-03-18 07:05:40

标签: file-io excel-vba input vba excel

我想读取由c写的文件,其中每行由/ n分隔。我想读取该文件并将其与excel上的数据进行比较。 我用了input #1, data。 但我想用“,”(逗号)读一行,所以我使用了Line Input #1, data

当我用excel上的数据检查“数据”时,虽然它们是相同的,但它的说法是假的。

Activecell="KVK"
Line Input #1,data
msgbox ActiveCell=data
即使数据是KVK,

也会打印错误。

感谢并提前帮助, Vamshi krishna

Dim fpath, fnum, s
fpath = Application.GetOpenFilename
fnum = FreeFile
Open fpath For Input As fnum
Range("A1").Activate

Do While Not EOF(fnum)
Line Input #fnum, s
'Input #fnum, s

MsgBox s & " = " & ActiveCell & "  "
MsgBox s = ActiveCell
ActiveCell.Offset(1, 0).Select
Loop

.txt

12
13
14

第一栏中的数据

12
13
14

2 个答案:

答案 0 :(得分:4)

尝试以下代码:

Sub InputImage()

    Dim FileNum As Integer, i As Integer
    Dim fpath As String, s As String, cellVal As String

    fpath = Application.GetOpenFilename


    FileNum = FreeFile()
    Open fpath For Input As #FileNum

    i = 1
    While Not EOF(FileNum)
        Line Input #FileNum, s    ' read in data 1 line at a time

        cellVal = CStr(Cells(i, 1).Value)

        MsgBox s & " = " & cellVal & "  "
        MsgBox s = cellVal
        ActiveCell.Offset(1, 0).Select

        i = i + 1
    Wend


End Sub

如果您在监视窗口中检查单元格的数据类型(i,1).Value显示Variant / Double。所以需要转换成字符串。

enter image description here

答案 1 :(得分:0)

或者使用Scripting库中的TextStream,它要好得多。

当你完成它时请关闭文件,你必须是那种在完成它后不将牛奶放回冰箱的人并且只为每个人破坏它。