Consulting

Results 1 to 9 of 9

Thread: Create 2 Dimensional Array from "For" Loop

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    Create 2 Dimensional Array from "For" Loop

    I'm pulling pairs of strings from text files and I need to store them in a 2-D array for each file. The number of these string pairs in each file will vary.

    The resulting array should be like below for a typical file.

    str_1               str_2
    --------------------------
    National Bank      123as
    Royal Bank         456vc
    Eastern Bank       789sd

    The loop is below returns the strings correctly:

        str_1 = Split(txt, "Interest Holder Name:")
        str_2 = Split(txt, "Document Reference:")
        
    For i = LBound(str_1) To UBound(str_1) - 1
            
            str_1 = Trim(Split(Split(txt, "Interest Holder Name:")(i + 1), " " & vbCrLf)(0))
            
            str_2 = Split(Trim(Split(txt, "Document Reference:")(i + 1)), " ")(0)
                
         Next i
    How do I store the strings in a 2-D array? Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a sample of your text file.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    M_snb()  
        str_1 = Split(txt, "Interest Holder Name:")
        str_2 = Split(txt, "Document Reference:")    
    
        redim sp(ubound(str_1),1)
        For j = 0 To UBound(str_1) - 1            
           sp(j,0) = Trim(Split(str_1(j), " " & vbCrLf)(0))            
           sp(j,1) = Split(str_2(j), " ")(0)
        Next
    end sub

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Uploaded a sample txt file
    Attached Files Attached Files

  5. #5
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    @SNB - could you provide some explanation / comment on what's happening in code - I'm reading up on Arrays, but am new to them.

    Any declarations required? How do I check string values - debug.print not returning - I guess because an array?

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    When you have a follow up question, please provide a link to that question
    Sub Test()
        Dim objFSO As Object
        Dim objTF As Object
        Dim strIn 'As String
        Dim txt
        Dim arr()
         
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTF = objFSO.OpenTextFile("C:\VBAX\Test_4.txt", 1)
        strIn = objTF.readall
             
        strt = InStr(1, strIn, "RECORDED INTERESTS AND INSTRUMENTS")
        endd = InStr(1, strIn, "NON-ENABLING INSTRUMENTS")
             
        txt = Mid(strIn, strt, endd - strt)
              
        Do Until InStr(txt, "  ") = 0 ' Loop until there are no more double spaces
            txt = Replace(txt, "  ", " ") ' Replace 2 spaces with 1 space
        Loop
             
        strIntHlder = Split(txt, "Interest Holder Name:")
        ReDim arr(UBound(strIntHlder), 1)
        
        
        For i = LBound(strIntHlder) To UBound(strIntHlder) - 1
            arr(i, 0) = Trim(Split(Split(txt, "Interest Holder Name:")(i + 1), " " & vbCrLf)(0))
        Next i
         
        strDocRef = Split(txt, "Document Reference:")
        For i = LBound(strDocRef) To UBound(strDocRef) - 1
            arr(i, 1) = Split(Trim(Split(txt, "Document Reference:")(i + 1)), " ")(0)
        Next i
        
        Cells(1, 1).Resize(i, 2) = arr
        
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Will link follow up questions going forward, and thanks very much for the code ... need to spend a bit of time with this now

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Specifically written for you: http://www.snb-vba.eu/VBA_Arrays_en.html


    For your file this code suffices:

    Sub M_snb()
       sn = Filter(Split(Replace(Replace(CreateObject("scripting.filesystemobject").opentextfile("G:\OF\test_4.txt").readall, "Interest Holder Name: ", "@"), "Document Reference: ", "@"), vbCrLf), "@")
       ReDim sp(UBound(sn) \ 2, 1)
       
       For j = 0 To UBound(sn) Step 2
          sp(j \ 2, 0) = Mid(sn(j), 2)
          sp(j \ 2, 1) = Mid(sn(j + 1), 2)
       Next
       
       Cells(1).Resize(UBound(sp) + 1, UBound(sp, 2) + 1) = sp
    End Sub
    Last edited by snb; 08-11-2017 at 12:30 AM.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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