视觉基础Excel txt输入文件 [英] Visual Basics Excel txt input file

查看:177
本文介绍了视觉基础Excel txt输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行输入#1,数字(行,列)给我一个运行时错误9.任何想法?代码中的注释解释了应该发生的情况。

Line "Input #1, Numbers(Row, Column)" is giving me a runtime bug 9. Any ideas? The comments in the code explain what's supposed to happen.

Sub InputImage()
'brings a text file to the workbook

Dim Row As Long, Column As Long, Nrow As Long, Ncolumn As Long

    Call MsgBox("Navigate to a folder that contains the file image.txt in the 00_18 folder")
    '   The next statement will open a dialogue box that will let
    '   the user navigate to any folder on their system
    Call Application.GetOpenFilename
Open "image.txt" For Input As #1

Dim Buffer As String
Line Input #1, Buffer 'read a whole line of characters from
    'file #1 into the string variable buffer

Line Input #1, Buffer
Input #1, Buffer, Nrow
Input #1, Buffer, Ncolumn

Dim Numbers(1 To 4, 1 To 4) As Long
For Row = 1 To Nrow
    For Column = 1 To Ncolumn
        Input #1, Numbers(Row, Column)
    Next Column
Next Row

Close #1

End Sub


推荐答案

请尝试以下代码:

Option Base 1
Sub InputImage()
'brings a text file to the workbook
    Dim j As Integer, i As Integer
    Dim Row As Long, Column As Long, Nrow As Long, Ncolumn As Long

    Call MsgBox("Navigate to a folder that contains the file image.txt in the 00_18 folder")
    '   The next statement will open a dialogue box that will let
    '   the user navigate to any folder on their system
    Call Application.GetOpenFilename


    Dim FileNum As Integer
    Dim DataLine As String

    FileNum = FreeFile()
    Open "image.txt" For Input As #FileNum


    j = 1
    While Not EOF(FileNum)
        Line Input #FileNum, DataLine    ' read in data 1 line at a time
        ' decide what to do with dataline,
        ' depending on what processing you need to do for each case
        'you can use split function to split DataLine with required delimiter which will return you an array.
        a = Split(DataLine, vbTab)

        For i = 1 To UBound(a)
            Cells(j, i).Value = a(i)
        Next
        j = j + 1
    Wend


End Sub

假设您的image.txt如下图所示。

Assuming your image.txt is like below image.

这篇关于视觉基础Excel txt输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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