Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String VBA脚本始终返回超过文件结尾错误的输入_String_Vba_Csv_Ms Word - Fatal编程技术网

String VBA脚本始终返回超过文件结尾错误的输入

String VBA脚本始终返回超过文件结尾错误的输入,string,vba,csv,ms-word,String,Vba,Csv,Ms Word,我有一个csv文件: hello,world,this is,an,example, of,the,csv,file 第一列将始终是唯一的 我让用户输入与第一列中的项目匹配的字符串,脚本返回整行: Open "C:\file1.csv" For Input As #file_number Do While (EOF(1) Or found = False) Line Input #file_number, raw_line pos = InStr(raw_line, fireN

我有一个csv文件:

hello,world,this
is,an,example,
of,the,csv,file
第一列将始终是唯一的

我让用户输入与第一列中的项目匹配的字符串,脚本返回整行:

Open "C:\file1.csv" For Input As #file_number
Do While (EOF(1) Or found = False)
    Line Input #file_number, raw_line
    pos = InStr(raw_line, fireName)
    If pos = Not 0 Then
        strData() = Split(raw_line, ",")
        found = True
    End If
Loop
Close #file_number
If found = False Then
    MsgBox "Could not find it"
    Exit Sub
End If
'REST OF THE CODE
但它总是发送消息“找不到”并退出。 默认情况下,“找到”为布尔值,为false

我知道它可以检测文件,因为当我更改读取文件名时,它创建了一个新文件(因为它不存在)

编辑: 我将
更改为
,现在出现错误:
运行时错误62:输入超过文件结尾

执行While
循环中,这看起来像是一个简单的错误。检查EOF,而不是EOF,因为循环从BOF开始,因此永远不会执行

类似的东西应该可以工作(我的语法可能有点不正确,因为我有一段时间没有使用VBA了)

打开“C:\file1.csv”作为#文件号输入
而不是EOF(文件编号),并且发现为False
行输入#文件号,原始行
pos=InStr(原始线,火焰名称)
如果位置为0,则
strData()=拆分(原始行,“”)
找到=真
如果结束
温德
关闭#文件编号
如果find=False,则
MsgBox“找不到它”
出口接头
如果结束

如果pos!=VBA中的0是

If pos <> 0
如果位置0

现在它可以工作了

可能不会产生任何问题(因为您大概只打开了一个文件),但是
EOF(1)
检查硬编码的文件编号,然后您将其作为#文件编号打开。它应该是
EOF(file\u number)
@Comintern当我尝试这种方式时,我只打开了一个文件,即使找不到这个词,它也不会发送msgboxI。我在
语句中添加了
not
,如果pos=
语句,它现在更有意义了,但现在它总是返回
找不到它的消息。
If pos <> 0