VBA脚本总是返回输入过去结束文件错误 [英] VBA script always returns Input past end of file error

查看:295
本文介绍了VBA脚本总是返回输入过去结束文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件:

  hello,world,this 
是,
of,the,csv,file

第一列总是唯一的。 p>

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

 打开C:\file1.csv作为输入as #file_number 
Do While(EOF(1)Or found = False)
线输入#file_number,raw_line
pos = InStr(raw_line,fireName)
如果pos =非0那么
strData()= Split(raw_line,,)
found = True b $ b结束如果
循环
关闭#file_number
如果found = False则
MsgBox找不到
退出Sub
结束如果
'代码的REST

但它总是发送消息could not find it退出。
默认情况下,found是一个布尔值,为false。



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



编辑:
我将更改为现在我得到的错误:
运行时错误62:输入文件结束

解决方案

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



这样的东西应该工作(我的语法可能稍微关闭,因为我没有使用VBA)

 打开C:\file1.csv #file_number 
当不是EOF(file_number)并且发现<> False
行输入#file_number,raw_line
pos = InStr(raw_line,fireName)
如果pos<> 0 then
strData()= Split(raw_line,,)
found = True
如果
结束
关闭#file_number
如果找到= False Then
MsgBox找不到
退出Sub
结束如果


I have a csv file as such:

hello,world,this
is,an,example,
of,the,csv,file

The first column will always be unique.

I am getting the user to enter a string which matches an item in the first column, and the script returns the whole line:

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

But it always sends the message "could not find it" and exits. By default, found is a boolean value and is false.

I know it can detect the file as when I changed the read file name, it created a new one (as it does not exist).

EDIT: I changed the And to an Or and now I get the error: Run-time error 62: input past end of file

解决方案

It looks like a simple error in your Do While loop. You Check for the EOF instead of not the EOF, as such the loop will never execute since it starts at the BOF.

Something like this should work (my syntax may be slightly off as I haven't used VBA in a while)

Open "C:\file1.csv" For Input As #file_number
While Not EOF(file_number) And found <> False
    Line Input #file_number, raw_line
    pos = InStr(raw_line, fireName)
    If pos <> 0 Then
        strData() = Split(raw_line, ",")
        found = True
    End If
Wend
Close #file_number
If found = False Then
    MsgBox "Could not find it"
    Exit Sub
End If

这篇关于VBA脚本总是返回输入过去结束文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆