Copy Range of Cells with Scripting Dictionary

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
701
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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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