Scripting Dictionary

rsutton1981

New Member
Joined
Mar 9, 2016
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi,

using some code that was posted on these forums and some modifications I have some VBA that looks for a specific value in Column B, then finds the rows with the same value in a different sheet (Database), copies them and pastes them to another sheet (Risk Selection). However, when it moves to the next value in column B and does the look up it pastes the data over, it places it over the previously places data rather then using an empty row.

Can anyone see where I may have missed something in the code

VBA Code:
Sub search()
   Dim Dic As Object
   Dim Cl As Range
   Dim wsRA As Worksheet
   
   Set wsRA = Sheets("Risk Assessment")
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Database")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not Dic.Exists(Cl.Value) Then
            Dic.Add Cl.Value, Cl
         Else
            Set Dic(Cl.Value) = Union(Cl, Dic(Cl.Value))
         End If
      Next Cl
   End With
   With Sheets("Risk Selection")
      For Each Cl In .Range("B6", .Range("B" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then
            Dic(Cl.Value).EntireRow.Copy wsRA.Range("A" & Rows.Count).End(xlUp).Offset(1)
         End If
      Next Cl
   End With
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Does column A on the Risk sheet have values on every row?
 
Upvote 0
In that case use
VBA Code:
Dic(Cl.Value).EntireRow.Copy wsRA.Range("B" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Oops, should be
VBA Code:
Dic(Cl.Value).EntireRow.Copy wsRA.Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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