Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate data based on word using dictionary

0

Hello,

I would extract data across  sheets to MASTER sheet contain APPLICATION word.

I know there is simple way to do that, but I have curiosity how use  by dictionary .

so after I got this code from the internet I changed somethings, but I note will just brings data  from BB sheet and ignores the others sheets contain APPLICATION wod .

I hope some body fix the code .

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi again Speed

Using a dictionary is a bit tricky but in the modified code below, I've got an input from the user then provided case-insensitive search results*, with Column G showing where the text was found (and G1 stating what text was searched for):

Sub test()
    Dim ws, a, i As Long, w, dic As Object
    Dim Srch As String, TempArray As Variant

    Set dic = CreateObject("Scripting.Dictionary")

    ' Get word from user and convert
    Srch = LCase(InputBox("Enter word to find", , "Application"))
    ' do nothing unless > 3 chars
    If Len(Srch) < 4 Then
        MsgBox "Try again with a word of 3 letters of more"
        Exit Sub
    End If

    For Each ws In Worksheets
     If ws.Name <> "MASTER" Then

         a = ws.Cells(1).CurrentRegion.Value
         For i = 2 To UBound(a, 1)
             ' compare Lower case value with Srch
             If LCase(a(i, 2)) = Srch Then
                 'collect values
                 w = Application.Transpose(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4), a(i, 5), a(i, 6), ws.Name & " cell B" & i))

                 If Not dic.exists(a(i, 2)) Then
                     ' add key and add values as item for found location
                     dic.Add Key:=a(i, 2), Item:=w
'
                 Else
                   ' otherwsier add to item
                   TempArray = dic(a(i, 2))
                    ReDim Preserve TempArray(1 To UBound(TempArray, 1), 1 To UBound(TempArray, 2) + 1)

                    ' add values to existing item
                    For k = 1 To UBound(TempArray, 1)
                       TempArray(k, UBound(TempArray, 2)) = w(k, 1)
                    Next k

                   dic(a(i, 2)) = TempArray
                 End If
             End If
         Next i
          End If
         Next ws

 With Sheets("MASTER")
    .Cells(1).CurrentRegion.Offset(1).ClearContents

    ' loop through dictionary
    For Each Key In dic

        TempArray = Application.Transpose(dic(Key))
        On Error Resume Next
        ' determine where to paste item values
        FreeRow = .Range("A" & .Rows.Count).End(xlUp).Row
        EndRow = FreeRow + UBound(TempArray, 1)
        ' correct for only one dictionary item
        If IsError(UBound(TempArray, 2)) Then
            Application.Transpose (TempArray)
            EndRow = FreeRow + 1
        End If
        With .Range("A" & FreeRow + 1 & ":G" & EndRow)
            .Value = TempArray
        End With
    Next Key

    ' clear and add borders
    .UsedRange.Borders.LineStyle = xlNone
    .Cells(1).CurrentRegion.Borders.LineStyle = xlContinuous
    ' write to G1
    .Cells(1, 7).Value = "Column G (Found Locations for text " & StrConv(Srch, vbProperCase) & ")"
  End With

    ' release memory
    Set dic = Nothing: w = Nothing
End Sub

* You'll see that I changed some instances of "Application" (e.g. to "APplication").

Hope this helps- if so, please select this Answer as Selected. 

Discuss

Discussion

Big work ,John!
I thought my code just lacks a simple something, but seem  I was wrong.
thank you so much 
speed (rep: 46) Apr 29, '25 at 4:19 am
Glad that helped. I made it a bitv ricky by sticking with the array approach in your original code and by making it case-insensitive.. Thanks again for selecting me Answer, Speed. 
John_Ru (rep: 6722) Apr 29, '25 at 6:18 am
Add to Discussion
0

Hi speed,

John has provided you with a good solution using the "dictioary" method.

I am not familiar with using "dictionary" so I am posting simpler code which checks each sheet for "Application" in column "B" and then copies that record (columns A thru F) to the next row of the "MASTER" sheet.

Option Explicit
'

Sub Copy_Matches()

Dim LrowMaster As Long
Dim ws As Worksheet
Dim LRws As Long
Dim a As Long

LrowMaster = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row

For Each ws In Worksheets
    If ws.Name <> "MASTER" Then
    ws.Activate
    LRws = ws.Cells(Rows.Count, 1).End(xlUp).Row
    ws.Activate
        For a = 2 To LRws
                If Cells(a, 2) = "Application" Then
                    Range(Cells(a, 1), Cells(a, 6)).Copy _
                        destination:=Sheets("MASTER").Cells(LrowMaster + 1, 1)
                        LrowMaster = LrowMaster + 1
                End If
        Next
        Cells(1, 1).Select
    End If
Next ws

End Sub
'

Cheers   :-)

Discuss

Discussion

Thanks  willie for simple code.
by the way you need this line as bold
Sheets("MASTER").[A1].CurrentRegion.Offset(1).ClearContents
 
LrowMaster = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row

because your code repeat copying same data have already existed to the bottom.
speed (rep: 46) Apr 29, '25 at 4:43 am
Thanks for that speed. I had given this some consideration but left it out.
Rather than deleting everything, I have opted to remove duplicates after the records are copied by using the following at the end of the code:
LrowMaster = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("MASTER").Range("$A$1:$F$" & LrowMaster).RemoveDuplicates _
        Columns:=Array(1, 2, 3, 4, 5, 6), Header:=xlYes


Cheers   :-)
WillieD24 (rep: 687) Apr 29, '25 at 3:24 pm
Thanks Willi,
I don'n think to be work !
repeat copying same data have already existed to the bottom.
speed (rep: 46) May 1, '25 at 12:19 pm
@speed,

That's one of the beautiful things about Excel - almost always there is more than one way to achieve the same results.

Cheers   :-)
WillieD24 (rep: 687) May 1, '25 at 7:23 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login