输入文件VBA的最后一个excel

输入文件VBA的最后一个excel

本文介绍了输入文件VBA的最后一个excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下宏来读取文本文件(这些文件由外部程序生成,不能被调整)。

 而不是EOF(int_current_file)
行输入#int_current_file,buf
如果Left(buf,1 )< #然后
buf1 = Split(buf,=)
打印#int_master_file,CInt(Left(buf,2))& ; &安培; CInt(Mid(buf,3,4))& ; &安培; CInt(Mid(buf,7,3))& ; &安培; CInt(Mid(buf,10,1))& ; &安培; CDbl(buf1(1)/ 100000000)& ; &安培; CDate(file_date)& ; &安培; Mid(file_name,4,3)
End If
'Line Input #int_current_file,buf
'Debug.Print Data
Wend
但是,在这个文件的第二行,我有如下字符串:



= 01082013 = 01072013 = 31072013 = 06082013 = 1640 = 380441 = 21 = 000001249 =#02IFS86.G84 = IFSSS5 =7ҐKЂi-Љ42ЃЁ4№{¤Хo$]ґ•ХpЁ1<;!±〜†ЁRLЌг‰当ќWhenќќќќ‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ the the c $ c>发生输入文件末尾



我如何解决这个问题?

解决方案

你更好地阅读VBA中的文本文件?



这将读取 ONE GO 中的整个文本文件在一个数组中,然后关闭该文件。这样你就不用随时打开档案。

  Option Explicit 

Sub Sample()
Dim MyData As String,strData()As String
Dim i As Long

'~~>在这里替换你的文件
打开C:\MyFile.Txt对于二进制为#1
MyData =空格$(LOF(1))
获取#1,MyData
关闭#1
strData()=拆分(MyData,vbCrLf)

'
'~~>现在strData具有文本文件的所有数据
'

对于i = LBound(strData)到UBound(strData)
Debug.Print strData(i)
'
'~~>你想要什么
'
Next i
End Sub


I am attempting to read text file (these files are produced by external program, that could not be tweeked) using the following macro.

    While Not EOF(int_current_file)
    Line Input #int_current_file, buf
    If Left(buf, 1) <> "#" Then
        buf1 = Split(buf, "=")
        Print #int_master_file, CInt(Left(buf, 2)) & ";" & CInt(Mid(buf, 3, 4)) & ";" & CInt(Mid(buf, 7, 3)) & ";" & CInt(Mid(buf, 10, 1)) & ";" & CDbl(buf1(1) / 100000000) & ";" & CDate(file_date) & ";" & Mid(file_name, 4, 3)
    End If
    'Line Input #int_current_file, buf
    'Debug.Print Data
Wend

However, at the second line of this file I have the following string:

=01082013=01072013=31072013=06082013=1640=380441=21=000001249=#02IFS86.G84=IFSSS5=7ҐK!Ђi—Љ42ЃЁ4№{¤Хo$]ґ•Хp Ё1‹;±~†ЁRLЌг‰®ґн нќРР^±>_‰

When macro tries to read this line the error 62 occurs Input past end of file.

How can I fix this problem?

解决方案

May I interest you in a better way of reading text files in VBA?

This will read the entire text file in ONE GO in an array and then close the file. This way you don't need to keep the file open at all times.

Option Explicit

Sub Sample()
    Dim MyData As String, strData() As String
    Dim i As Long

    '~~> Replace your file here
    Open "C:\MyFile.Txt" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    strData() = Split(MyData, vbCrLf)

    '
    '~~> Now strData has all the data from the text file
    '

    For i = LBound(strData) To UBound(strData)
        Debug.Print strData(i)
        '
        '~~> What ever you want here
        '
    Next i
End Sub

这篇关于输入文件VBA的最后一个excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:59