Copy Range of Cells with Scripting Dictionary

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I have 2 worksheets within the same workbook. I'm trying to copy a range of cells from mFS and paste them into mWD where a key exists on both sheets. I didn't want to add 24 VLOOKUP formulas with VBA, so I thought I would try my hand at a scripting dictionary (which may not be the best route either). Here is the code that I have now, which works, but I'm unsure how to copy the range of cells over instead of just 1 cell at a time. Thoughts?
VBA Code:
Sub MergeData()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim m As Workbook
Dim mWD, mFS As Worksheet
Dim WDLR, FSLR As Long

Dim C1 As Range
Dim Dic As Object

Set m = ThisWorkbook
Set mWD = m.Sheets("Working_Data")
Set mFS = m.Sheets("From_SPARC")

WDLR = mWD.Range("A" & Rows.Count).End(xlUp).Row

FSLR = mFS.Range("B" & Rows.Count).End(xlUp).Row

mFS.Range("A2:A" & FSLR).Formula = "=RC[1] & "" | "" & RC[3] & "" | "" & RC[4] & "" | "" & RC[5]"

mWD.Range("M2:M" & WDLR).Formula = "=RC[-12] & "" | "" & RC[-8] & "" | "" & RC[-9] & "" | "" & RC[-6]"

Set Dic = CreateObject("scripting.dictionary")
With mFS
    For Each C1 In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        Dic(C1.Value) = C1.Offset(, 6).Value
    Next C1
End With

With mWD
    For Each C1 In .Range("M2", .Range("A" & Rows.Count).End(xlUp))
        If Dic.exists(C1.Value) Then C1.Offset(, 1).Value = Dic(C1.Value)
    Next C1
End With

Application.DisplayAlerts = False
Application.ScreenUpdating = False

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You need to use
VBA Code:
With mWD
    For Each C1 In .Range("M2:M" & .Range("A" & Rows.Count).End(xlUp).Row)
        If Dic.exists(C1.Value) Then C1.Offset(, 1).Value = Dic(C1.Value)
    Next C1
End With
 
Upvote 0
You need to use
VBA Code:
With mWD
    For Each C1 In .Range("M2:M" & .Range("A" & Rows.Count).End(xlUp).Row)
        If Dic.exists(C1.Value) Then C1.Offset(, 1).Value = Dic(C1.Value)
    Next C1
End With
I made that change, but the results weren't different (I'm still only getting 1 column to copy & paste over to mWD from mFS. The values that I want to copy and paste start with the C1.Offset(,6). The issue is, I want to copy the next 23 columns of data. Something like Range("G:AK")
 
Upvote 0
Which cells should be copied & where to?
 
Upvote 0
Ok, how about
VBA Code:
Set Dic = CreateObject("scripting.dictionary")
With mFS
    For Each C1 In .Range("A2", .Range("A" & Rows.count).End(xlUp))
        Dic(C1.Value) = C1.Offset(, 6).Resize(, 24)
    Next C1
End With

With mWD
    For Each C1 In .Range("M2:M" & .Range("A" & Rows.count).End(xlUp).Row)
        If Dic.Exists(C1.Value) Then C1.Offset(, 1).Resize(, 24).Value = Dic(C1.Value)
    Next C1
End With
 
Upvote 0
Ok, how about
VBA Code:
Set Dic = CreateObject("scripting.dictionary")
With mFS
    For Each C1 In .Range("A2", .Range("A" & Rows.count).End(xlUp))
        Dic(C1.Value) = C1.Offset(, 6).Resize(, 24)
    Next C1
End With

With mWD
    For Each C1 In .Range("M2:M" & .Range("A" & Rows.count).End(xlUp).Row)
        If Dic.Exists(C1.Value) Then C1.Offset(, 1).Resize(, 24).Value = Dic(C1.Value)
    Next C1
End With
Works like a champ! So the resize portion is what says how many columns after the offset position to move over. Is that correct?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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