Pass Ranges To Dictionary and Resize Ranges

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
I want to load some ranges into a dictionary and then resize the ranges.

I was originally passing the rng.address to Dictionary but realized that I needed access to the Resize method of the range to resize it. So I switched to adding the range itself.

I am getting a runtime error 424 Object Required in the For Each loop. How do I loop through the actual range variables that I have added to the Dictionary to access and edit their cell addresses?

VBA Code:
Sub TEST()
Dim rnglblcoll as Dictionary
Dim rng as Range

Set rnglblcoll = New Scripting.Dictionary

Set rng = Range("NamedRange1")
rnglblcoll.Add item:=rng, Key:="Label1"
Set rng = Nothing
Set rng = Range("NamedRange2")
rnglblcoll.Add item:=rng, Key:="Label2"
Set rng = Nothing
Set rng = Range("NamedRange3")
rnglblcoll.Add item:=rng, Key:="Label3"

'Update Address in Dictionary
For Each rng In rnglblcoll
    rng = rng.Address.Resize(1, 1)
Next

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Why not just pass the correct range to start with
VBA Code:
Sub TEST()
Dim rnglblcoll As Dictionary


Set rnglblcoll = New Scripting.Dictionary


rnglblcoll.Add Range("NamedRange1").Resize(1, 1), "Lable1"
rnglblcoll.Add Range("NamedRange2").Resize(1, 1), "Label2"
rnglblcoll.Add Range("NamedRange3").Resize(1, 1), "Label3"



End Sub
 
Upvote 0
Thanks.

Quick question, if I wanted to do this in a loop after-the-fact, is it possible to access the range properties and methods?

The reason I ask is I have a large number of ranges to add and I want to avoid hard-coding the variables in the Resize method (i.e Resize(1,1),Resize(2,1),Resize(3,1) . If I put it in a loop I can use a counter variable (i.e. Resize.cells(Counter,1))

Thanks
 
Upvote 0
Depends on what you are trying to do, but you could use something like
VBA Code:
Sub TEST()
Dim rnglblcoll As Dictionary
Dim i As Long

Set rnglblcoll = New Scripting.Dictionary


rnglblcoll.Add Range("NamedRange1"), "Lable1"
rnglblcoll.Add Range("NamedRange2"), "Label2"
rnglblcoll.Add Range("NamedRange3"), "Label3"
For i = 1 To 10
   Debug.Print rnglblcoll.Keys(1).Cells(i, 1).Address, rnglblcoll.Keys(1).Resize(i, 1).Address
Next i

End Sub
 
Upvote 0
I am actually trying to redefine the range of the named address so that I can paste it dynamically in a spreadsheet that has changing rows.

So I will eventually be dong this:

Range("NamedRange1") = DATA from an Array

What I am doing upfront is putting this range and a few others with their existing cell addresses into a dictionary. I then want to loop through the collection and change the addresses based on some criteria. When it comes time to printing, I want to loop through that same collection and access the redefined addresses as the print.

Thanks
 
Upvote 0
Wouldn't it be easier to just delete the ranges, paste the new data in & then recreate the named ranges?
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,762
Members
449,048
Latest member
excelknuckles

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