+ Reply to Thread
Results 1 to 16 of 16

Save Txt file to a one dimensional array

Hybrid View

Alexander40 Save Txt file to a one... 05-15-2024, 02:50 PM
TMS Re: Save Txt file to a one... 05-15-2024, 03:23 PM
Alexander40 Re: Save Txt file to a one... 05-16-2024, 09:25 AM
Alexander40 Re: Save Txt file to a one... 05-15-2024, 03:53 PM
AliGW Re: Save Txt file to a one... 05-16-2024, 09:28 AM
Alexander40 Re: Save Txt file to a one... 05-21-2024, 09:46 AM
TMS Re: Save Txt file to a one... 05-21-2024, 09:55 AM
JEC. Re: Save Txt file to a one... 05-21-2024, 11:14 AM
JEC. Re: Save Txt file to a one... 05-21-2024, 11:23 AM
Alexander40 Re: Save Txt file to a one... 05-21-2024, 01:17 PM
daboho Re: Save Txt file to a one... 05-23-2024, 10:01 AM
JEC. Re: Save Txt file to a one... 05-21-2024, 01:20 PM
JEC. Re: Save Txt file to a one... 05-21-2024, 01:23 PM
TMS Re: Save Txt file to a one... 05-22-2024, 06:25 PM
TMS Re: Save Txt file to a one... 05-22-2024, 06:31 PM
JEC. Re: Save Txt file to a one... 05-23-2024, 01:22 AM
  1. #1
    Registered User
    Join Date
    08-15-2008
    Location
    Mass
    MS-Off Ver
    2016, 64- Bit
    Posts
    36

    Save Txt file to a one dimensional array

    Hello,
    I want to be able to open a text file into an 1 dimensional array in excel. I don't want to split it, I just want each row to equal a line of text.

    Thanks in advance.

    Alex

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,085

    Re: Save Txt file to a one dimensional array

    Rather than re-invent the wheel, see: https://stackoverflow.com/questions/...n%20an%20array.

    Or Google: excel vba to read entire text file into an array for more suggestions.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-15-2008
    Location
    Mass
    MS-Off Ver
    2016, 64- Bit
    Posts
    36

    Re: Save Txt file to a one dimensional array

    didn't work

  4. #4
    Registered User
    Join Date
    08-15-2008
    Location
    Mass
    MS-Off Ver
    2016, 64- Bit
    Posts
    36

    Re: Save Txt file to a one dimensional array

    Getting an error on line

    Range("A1").Resize(UBound(Arr) + 1, 1).Value = Application.Transpose(Arr)

    that states: Run -time error '1004' " Application -defined or object -defined error

    Also, Not sure this is what I am looking for. I want to have each line as it's own "row" in the array. So Arr(0) would equal the first line of text, Arr(1) would equal the second line of text and so on.
    Last edited by Alexander40; 05-15-2024 at 03:58 PM.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,371

    Re: Save Txt file to a one dimensional array

    Provide a sample workbook.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  6. #6
    Registered User
    Join Date
    08-15-2008
    Location
    Mass
    MS-Off Ver
    2016, 64- Bit
    Posts
    36

    Re: Save Txt file to a one dimensional array

    Sub Test()
    
     Dim FSO As Object, MyFile As Object
     Dim FileName As String, Arr As Variant
    
     FileName = "Z:\xxxxxxx" ' change this to your text file full name
     Set FSO = CreateObject("Scripting.FileSystemObject")
     Set MyFile = FSO.OpenTextFile(FileName, 1)
     Arr = Split(MyFile.ReadAll, vbNewLine) ' Arr is zero-based array
    
    
     'For test
     'Fill column A from this Array Arr
    
     Range("A1").Resize(UBound(Arr) + 1, 1).Value = Application.Transpose(Arr)
    
    End Sub
    Last edited by AliGW; 05-21-2024 at 09:48 AM. Reason: Code tags added - please review the forum guidelines.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,085

    Re: Save Txt file to a one dimensional array

    Please provide a copy of the text file to be imported.

  8. #8
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Save Txt file to a one dimensional array

    It should work though. Change the path to yours

    Sub jec()
     Dim ar
     ar = Split(CreateObject("scripting.filesystemobject").opentextfile("C:\xxx\xxx\xxx.txt").readall, vbLf)
     Range("A1").Resize(UBound(ar) + 1, 1) = Application.Transpose(ar)
    End Sub

  9. #9
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Save Txt file to a one dimensional array

    Or

    Sub jecc()
     Dim ar
     Open "C:\xxx\xxx\xxx.txt" For Input As #1
     ar = Split(Input(LOF(1), 1), vbLf)
     Range("A1").Resize(UBound(ar) + 1, 1) = Application.Transpose(ar)
     Close #1
    End Sub

  10. #10
    Registered User
    Join Date
    08-15-2008
    Location
    Mass
    MS-Off Ver
    2016, 64- Bit
    Posts
    36

    Re: Save Txt file to a one dimensional array

    Thanks JEC,
    But it doesn't pull in the entire file. There are over 1 million rows in the text file, but when I do a ubound on the ar array it only has 18962 rows.

  11. #11
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Save Txt file to a one dimensional array

    Try using longlong for 64 bit
    Sub perhab()
     Dim a,i as longLong,j as longlong,c as long,k,n as long
     a = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\your Path").ReadAll, vbLf)
     Redim b (1 to rows.count,1 to 400)
     c = 1
     For i = 0 to Ubound(a)-1
       If j > rows.count-2 then
         j = 1 : c = c + 1
       End if
       j = j + 1 : k = j : n = application.Max(k,n)
       b(k,c) = a(i)
     Next i
     [A1].resize(n,c).value = b
      Erase b
    End sub
    Last edited by daboho; 05-23-2024 at 10:05 AM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  12. #12
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Save Txt file to a one dimensional array

    The ubound should show the correct result. The application.transpose on the other hand, can’t handle so much data. Therefore you need another approach. Can you post it?

  13. #13
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Save Txt file to a one dimensional array

    You should try to use Power Query

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,085

    Re: Save Txt file to a one dimensional array

    Subroutines to read a complete/entire text file

    Change the source file and location as required.

    Try this:

    Option Explicit
    
    ' Subroutines to read a complete/entire text file into:
    ' 1. A variant variable and then split it
    ' 2. A cell on a worksheet
    '
    ' Both use the ReadTextFile User Defined Function
    '
    
    Sub Test_ReadTextFile_Into_Variable()
    
    Dim awf As WorksheetFunction: Set awf = Application.WorksheetFunction
    Dim vInput, vArray
    
    ' change file name and location as required
    vInput = ReadTextFile("C:\Test\General Toy Sale 26-05-2022.csv")
    vArray = Split(vInput, Chr(10))
    
    Range("A1").Resize(UBound(vArray)) = _
        awf.Transpose(vArray)
    With Range("A1")
        .ColumnWidth = 120
        .EntireColumn.Rows.AutoFit
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    End Sub
    
    Sub Test_ReadTextFile_Into_Cell()
    
    With Range("A1")
        ' change file name and location as required
        .Value = ReadTextFile("C:\Test\General Toy Sale 26-05-2022.csv")
        .ColumnWidth = 120
        .EntireColumn.Rows.AutoFit
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    End Sub
    
    
    ' Uncredited as, unfortunately, I have no idea where I got this routine.
    ' I found it in a solution for rizmomin back in 2014 
    
    ' Sometimes you just want to read in a complete text file
    ' from your PC without muddling through all the records
    ' (i.e. text lines) one by one. When you open a text file
    ' in binary mode, such things like EOL won't get special
    ' attention and you can read in the text as one big chunk.
    '
    ' This function takes the path and name of a text file as
    ' input and returns the text of this file as a string:
    
    ' Reads a text file and returns the text as string
    ' i_FileName contains full name (i.e. with path) of the file to be read
    
    Public Function ReadTextFile(ByVal i_FileName As String) As String
    
    Dim myFNo As Integer  'file number to open file
    Dim myText As String  'string where text gets read into
    
    'only open file if it can be found
    If Dir(i_FileName, vbNormal) <> "" Then
        'obtain the next available file number
        myFNo = FreeFile
        'open file in binary mode
        Open i_FileName For Binary As #myFNo
        'initialize string to receive text with
        'as many spaces as the file has bytes
        myText = Space(LOF(myFNo))
        'read everything at once
        Get #myFNo, , myText
        'close file
        Close #myFNo
        'return text
        ReadTextFile = myText
    End If
    End Function
    Sample workbook and test file to play with. Also contains a query to load the test file.
    Attached Files Attached Files

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,085

    Re: Save Txt file to a one dimensional array

    Found the source for the function.

    LIVEJOURNAL
    VBA_CORNER
    Dated 24 August 2009

    https://vba-corner.livejournal.com/5311.html

  16. #16
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Save Txt file to a one dimensional array

    First sub is doing the same as my suggestions. But the transpose can handle around 36k records or less…
    Since here are over a million records, you can’t load it in one column at all.
    Last edited by JEC.; 05-23-2024 at 01:49 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-24-2023, 05:21 PM
  2. [SOLVED] have 1 dimensional array, trying to create a 2 dimensional array, runtime 9
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2019, 07:03 AM
  3. [SOLVED] Convert 2 dimensional array to 1 dimensional
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2018, 05:20 AM
  4. Help converting one-dimensional array to multi-dimensional array
    By puzzlelover22 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2016, 06:48 AM
  5. save one dimensional array to a range
    By HenT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-24-2016, 09:31 AM
  6. Parse Data from one dimensional array into a 2 dimensional array.
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-30-2016, 07:29 AM
  7. Creating a 2-dimensional array from a 1-dimensional list
    By guywithcamera in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-27-2008, 06:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1