0

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?

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.

3
  • what data will be in the second dimension? Commented Dec 10, 2016 at 1:04
  • the "..." represents numerous other lines in the file, I'm not sure what you mean by the data in the second dimension but following my example, if I used myArray(1,3) I want to be able to retrieve the string "asdf5" Commented Dec 10, 2016 at 1:06
  • Could you post what you have so far, saves reinventing the wheel. Commented Dec 10, 2016 at 1:22

1 Answer 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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! this works great, is there a way to disable the workbook that pops up for a split second from appearing?
Look into application.screenupdating
Application.DisplayAlerts = False
Don't use parentheses unless you are expecting a return value. wbtemp.Close False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.