VBA 线路输入与输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15471361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Line Input vs Input
提问by krishna
I want to read a file written by c where each line is seperated by /n. I want read that file and compare it with data on excel.
I used input #1, data
.
But I want to read a line with ","(comma), So i used Line Input #1, data
.
我想读取由 c 编写的文件,其中每行由 /n 分隔。我想读取该文件并将其与 excel 上的数据进行比较。我用过input #1, data
。但我想阅读带有“,”(逗号)的一行,所以我使用了Line Input #1, data
.
when i check that "data" with data on excel, though they are same, its saying false.
当我用excel上的数据检查“数据”时,尽管它们相同,但它说的是错误的。
Activecell="KVK"
Line Input #1,data
msgbox ActiveCell=data
is printing false even if data is KVK.
即使数据是 KVK,也会打印 false。
Thanks and Regards for Helping in advance, Vamshi krishna
感谢和问候提前提供帮助,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 has
.txt 有
12
13
14
data in first column
第一列数据
12
13
14
采纳答案by krishna
Try below code :
试试下面的代码:
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
If you check in watch window the data type of cell(i,1).Value is showing Variant/Double. So there is need to convert into string.
如果您在监视窗口中查看单元格 (i,1).Value 的数据类型,则会显示 Variant/Double。所以需要转换成字符串。
回答by user1644564
Or use the TextStream in the Scripting library, it's much better.
或者使用Scripting library中的TextStream,会好很多。
And please close the file when your finished with it, you must be the type of person who doesn't put the milk back in the fridge when your finished with it and just spoils it for everyone.
用完后请关闭文件,你一定是那种用完后不把牛奶放回冰箱,只是把它宠坏给每个人的人。