VBA - Add row of data to dictionary if condition met.

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

Any suggestions how I can amend the below code so that if the condition is true the entire row values are copied to the dictionary to be output to a worksheet at the end of the process.

This will run on over 100,000 rows of data so I was trying to find a way to prevent having to interact with the sheet on each loop iteration and figured that it would be quicker to store in memory then dump it into a sheet at the end of the loop

Code:
Sub Test()


    Dim c As Range
    Dim rng As Range
    Dim arrwords As Variant
    Dim aword As Variant
    Dim dict As Scripting.Dictionary
    Dim DictKey As Long


    Set dict = New Scripting.Dictionary
    
    Set rng = Sheet1.Range("A2:A" & Sheet1.Cells(Rows.Count, "A").End(xlUp).Row)
    
    arrwords = Array("apple", "pair")
    DictKey = 0


    For Each c In rng
        For Each aword In arrwords
            Debug.Print c
            If InStr(1, LCase(c.Value), aword, vbBinaryCompare) > 0 Then
                DictKey = DictKey + 1
                dict.Add DictKey, c.Row 
            End If
        Next aword
    Next c
    
    Sheet2.Cells(1, 1).Resize(dict.Count, 15).Value = (dict.Items)


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How many words do you need to filter on?
 
Upvote 0
Testing approximately 100,000 rows for about 20 different words that may be in the string
 
Upvote 0
How about
Code:
Sub Trebor8484()
   Dim Ary As Variant, AryWrds As Variant
   Dim i As Long, j As Long
   
   AryWrds = Array("Tom", "D1ck", "Harry")
   Ary = Sheet1.Range("A2", Sheet1.Range("A" & Rows.Count).End(xlUp)).Value2
   With CreateObject("Scripting.dictionary")
      For i = 1 To UBound(Ary)
         For j = 0 To UBound(AryWrds)
            If InStr(1, Ary(i, 1), AryWrds(j), vbTextCompare) > 0 Then
               .Item(Ary(i, 1)) = Empty
            End If
         Next j
      Next i
      Sheet1.Range("A1").AutoFilter 1, .keys, xlFilterValues
      Sheet1.AutoFilter.Range.Offset(1).EntireRow.Copy Sheet2.Range("A1")
      Sheet1.AutoFilterMode = False
   End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top