named range = named ranged

ndendrinos

Well-known Member
Joined
Jan 17, 2003
Messages
1,694
Named range “toplist” is contiguous [A1:L1]
Named range ‘mylist” is non contiguous
(“A24,B23,A16,D20,E12,F27,E16,A15,K14,L20,P21,M19")

On sheet1
I’m looking for code that looks something like this:
Code:
[mylist].Value = [toplist].Value

My understanding is that because one range is non contiguous there needs to be a loop
and I need assistance

something like this
Code:
Sub test()
Dim rngCell As Range
Dim lngCount As Long
Dim aTemp()

    ReDim aTemp(0)
    lngCount = 0
    
    For Each rngCell In [toplist]
        aTemp(UBound(aTemp)) = rngCell
        ReDim Preserve aTemp(UBound(aTemp) + 1)
    Next rngCell
    
    ReDim Preserve aTemp(UBound(aTemp) - 1)
   


    With Worksheets("sheet1")
        For Each rngCell In [mylist]
        rngCell = aTemp(lngCount)
        lngCount = lngCount + 1
    Next rngCell
End With
End Sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
got it to work like this:
Code:
Sub hello()
Dim rngCell As Range
Dim lngCount As Long
Dim aTemp()

    ReDim aTemp(1)
    lngCount = 0
    
    For Each rngCell In [toplist]
        aTemp(UBound(aTemp)) = rngCell
        ReDim Preserve aTemp(UBound(aTemp) + 1)
    Next rngCell
    
    ReDim Preserve aTemp(UBound(aTemp) - 0)
   


    With Worksheets("Search")
        For Each rngCell In [mylist]
        rngCell = aTemp(lngCount)
        lngCount = lngCount + 1
    Next rngCell
End With
End Sub

but is there no simplier way?
 
Upvote 0
Hello Richard ,
Trying your suggestion nothing happens, no error either
Thank you

Just noticed that it does work BUT pastes in range [M26:X26] (and M26 is not selected before firing the code)
 
Last edited:
Upvote 0
Hi Richard not sure what you mean but since I edited my last post I went back and found that I had an extra cell [M26] at the beginning of the named range "my list"
Now what happens is that the code pastes on range [A26:L24]

mylist is: =Search!$A$24,Search!$B$23,Search!$A$16,Search!$D$20,Search!$E$12,Search!$F$27,Search!$E$16,Search!$A$15,Search!$K$14,Search!$L$20,Search!$P$21,Search!$M$19

toplist is: =Search!$A$1:$L$1
 
Upvote 0
for anyone following this post having deleted the extra cell in named range "mylist"
I edited the code to this:
Code:
Sub hello()
Dim rngCell As Range
Dim lngCount As Long
Dim aTemp()

    ReDim aTemp(0)
    lngCount = 0
    
    For Each rngCell In [toplist]
        aTemp(UBound(aTemp)) = rngCell
        ReDim Preserve aTemp(UBound(aTemp) + 1)
    Next rngCell
    
    ReDim Preserve aTemp(UBound(aTemp) - 1)
   


    With Worksheets("search")
        For Each rngCell In [mylist]
        rngCell = aTemp(lngCount)
        lngCount = lngCount + 1
    Next rngCell
End With
End Sub

Richard's code is way simpler and if it works for him then I must have explained it badly.
 
Upvote 0
Sorry Nik I got confused which way round your named ranges were - try this instead:

Code:
Dim i As Long
Dim rngCell As Range

i = 1
 
For Each rngCell In [mylist]
 
  rngCell.Value = Range("toplist")(i)
 
  i = i + 1
 
Next

The above is taking the values in toplist and placing them in mylist. It would have been easier to understand had you not used named ranges!
 
Upvote 0
Richard,
Perfect revision to your solution.
On a personal note I would have you know that I am Greek
It is my "raison d'etre" to confuse people.
Have a great day
 
Upvote 0
On a personal note I would have you know that I am Greek
It is my "raison d'etre" to confuse people.
Have a great day

My best friend at work is also Greek and he lives by this ethos too ;-)

He's also a joker: when I and another colleague go to his wedding (in Athens) in September, he's already spread rumours among his Greek friends and family that me & this other colleague (another male) are an 'item'!!! I'm going to get him for this though...
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

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