excel VBA中文本文件的2D数组

时间:2022-08-10 21:34:49

I am opening a text file which contains text like this

我打开一个文本文件,其中包含这样的文本

asdf,zxcv,asdwqe,asdh,we5,dvsew,safhc
asdf8,asdf3,asdf4,asdf5,asdf6,asdf7,asdf2
....

I am trying to put this all into a 2d array, the only way I have been able to do anything with this so far is to read the file line by line and split that line with a comma into a 1D array and use the information that way. But I have no idea how I'm supposed to do this with a 2D array, I have googled and have found nothing that is helpful. is there a way I can put my 1d array into a 2d array or something?

我试图把这一切都放到一个二维数组中,到目前为止,我能够做到这一点的唯一方法就是逐行读取文件并用逗号将该行拆分成一维数组并使用办法。但是我不知道我应该如何使用2D数组做这个,我用Google搜索并找不到任何有用的东西。有没有办法可以将我的1d数组放入2d数组中?

Also I do not know how many strings there are per line(but all lines will have the same amount) nor how many lines there are in the file.

另外我不知道每行有多少个字符串(但所有行的数量都相同),也不知道文件中有多少行。

EDIT: To clarify, how I want it to work is for example if I do MsgBox myArray(1,3) I want "asdf5" to be displayed in that message box.

编辑:澄清,我希望它如何工作是例如,如果我做MsgBox myArray(1,3)我希望“asdf5”显示在该消息框中。

1 个解决方案

#1


3  

This should work :

这应该工作:

Sub arrayTest()

    Dim arrData
    Dim wbtemp      As Workbook


    '/ 2 = Comma (format parameter)

    Set wbtemp = Workbooks.Open("C:\temp\test.txt", False, True, 2)

    '/ Read in Array. range array is always 2D
    arrData = wbtemp.Worksheets(1).UsedRange
    wbtemp.Close (0)

    '/ Range array will always start from 1. No 0 base,
    '/ but given how less code one needs to write, its a fair trade off.

    MsgBox arrData(2, 4)

End Sub

#1


3  

This should work :

这应该工作:

Sub arrayTest()

    Dim arrData
    Dim wbtemp      As Workbook


    '/ 2 = Comma (format parameter)

    Set wbtemp = Workbooks.Open("C:\temp\test.txt", False, True, 2)

    '/ Read in Array. range array is always 2D
    arrData = wbtemp.Worksheets(1).UsedRange
    wbtemp.Close (0)

    '/ Range array will always start from 1. No 0 base,
    '/ but given how less code one needs to write, its a fair trade off.

    MsgBox arrData(2, 4)

End Sub