gpt4 book ai didi

excel - Visual Basics Excel txt 输入文件

转载 作者:行者123 更新时间:2023-12-04 21:06:32 25 4
gpt4 key购买 nike

“输入 #1,数字(行,列)”行给了我一个运行时错误 9。有什么想法吗?代码中的注释解释了应该发生的事情。

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 如下图所示。

enter image description here

关于excel - Visual Basics Excel txt 输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com