+ Reply to Thread
Results 1 to 7 of 7

VBA Find instance of text in cell from array values, return array's 2nd dimension value

  1. #1
    Registered User
    Join Date
    06-08-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    44

    VBA Find instance of text in cell from array values, return array's 2nd dimension value

    Greetings all.

    While the below works, my data range(s) will exceed a 100k rows. I suspect this would be better achieved comparing two dictionaries objects with both ranges?

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this !


    According to your attachment a VBA demonstration for starters to paste only to the Sheet2 (accounts) worksheet module :

    PHP Code: 
    Sub Demo1()
        
    Dim VR&, oCol As New CollectionS$()
            
    Sheet1.[A1].CurrentRegion
            
    For 2 To UBound(V):  oCol.Add V(R2), V(R1):  Next
            V 
    = [A1].CurrentRegion.Columns("B:C")
            
    Application.ScreenUpdating False
        
    For 2 To UBound(V)
            If 
    V(R2) = "Bank" Then
                S 
    Split(V(R1))
                
    Cells(R4) = oCol(S(UBound(S) - + (Asc(S(UBound(S) - 1)) > 90)))
            
    End If
        
    Next
            Application
    .ScreenUpdating True
            Set oCol 
    Nothing
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    06-08-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    44

    Re: VBA Find instance of text in cell from array values, return array's 2nd dimension valu

    Thanks Marc. Couple of questions as the above for my understanding, as it is advanced for a me.

    Why does the code require to be in the sheet module?
    What is happening with & and $ in the R and S variables?

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,240

    Re: VBA Find instance of text in cell from array values, return array's 2nd dimension valu

    I ran some performance tests on 800k records (by multiplying the available data).
    There are 6 procedures in the attached file.
    The first three enter the data cell by cell, the next three fill the array and finally fill the entire range. This is obviously more efficient than the first method.
    But I also compared the performance of using Collection and Dictionary. Dictionary is faster than Collection, but this test did not confirm that at all. It also turned out that using a loop to search for bank names also performed very well. It is a little slower than using Collection or Dictionary, but the fraction of a second difference on 800K records may be negligible.
    As we run all the tests, the worksheet shows approximate execution times for each procedure. More realistic times are shown in the Immediate window.
    I leave the choice of method to OP.

    Marc L,
    Please Login or Register  to view this content.
    Clever, very clever.

    Artik
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA Find instance of text in cell from array values, return array's 2nd dimension valu


    • In addition to Artik's post, using a Dictionary is not always the fastest / more efficient method
    as with huge data - ~ 500K unique items & more - it becomes slower than using a Collection …

    As yet demonstrated in this forum in some cases an optimized loop on array could be the best option, faster than using a Dictionary …

    • Thanks Artik for your kind appreciation !

    • Answers to DHHM post #3 :

    My VBA basics demonstration is just designed to be in the Sheet2 worksheet module
    as there is no reference to this worksheet, in case this worksheet is not active.

    & is just an old school coding to declare a Long variable as you can see in VBA help
    or just during the execution checking the variables within the VBE Locals window …


    Another VBA basics demonstration which can be located in a general / standard module like Module1 :

    PHP Code: 
    Sub Demo2()
        
    Dim ABVR&, L&
            
    Application.ScreenUpdating False
        With Sheet1
    .[A1].CurrentRegion.Rows
            A 
    = .Parent.Evaluate("A2:A" & .Count "&"" """)
            
    = .Range("B2:B" & .Count)
        
    End With
        With Sheet2
    .[A1].CurrentRegion
                V 
    = .Columns("B:C")
            For 
    2 To UBound(V)
                If 
    V(R2) = "Bank" Then
                    
    For 1 To UBound(A)
                        If 
    InStr(1V(R1), A(L1), 1Then .Cells(R4) = B(L1): Exit For
                    
    Next
                End 
    If
            
    Next
        End With
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 06-07-2022 at 08:29 AM.

  6. #6
    Registered User
    Join Date
    06-08-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    44

    Re: VBA Find instance of text in cell from array values, return array's 2nd dimension valu

    & is just an old school coding to declare a Long variable as you can see in VBA help
    or just during the execution checking the variables within the VBE Locals window …


    I wondered how it was doing that. Another thing I've learned today!

    Late in the pm over here so I will spend the next few days looking at Artik's file, and your suggestion above and understanding how each does what they do.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA Find instance of text in cell from array values, return array's 2nd dimension valu


    As Demo2 is just an optimized version of your original code …

    Thanks for the rep' !

+ 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. Re-Setting the dimension of an array to exclude #NA values
    By Najwa_X in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-14-2020, 12:51 PM
  2. Find text in array & return adjacent cell value
    By econbizer in forum Excel General
    Replies: 13
    Last Post: 10-13-2012, 01:26 AM
  3. [SOLVED] Find 3 largest values in an array and return corresponding text from header row
    By philrossnz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-21-2012, 01:17 AM
  4. Find a value in an array and return multiple values in an adjacent array
    By tonbra in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2012, 08:35 PM
  5. For Each Cell.Value within a 1D array, find each instance within a 2D array.
    By whilburn in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2010, 04:51 PM
  6. [SOLVED] Write 1st and 2nd dimension array values to cells
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2006, 07:20 PM
  7. HOW TO ASSIGN 2 DIMENSION ARRAY VALUES FROM 2 COLUMNS?
    By -JEFF- in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2005, 01:05 PM

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