Results 1 to 16 of 16

Save Txt file to a one dimensional array

Threaded View

  1. #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,099

    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

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