简体   繁体   中英

Excel VBA Array to non-contiguous named ranges

I'm trying to get data from an array into a non-contiguous named range. I keep ending up with the first cell of data is all of the named range cells. I think I hurt my brain. I'm looking at my code and not even sure why I wrote it, so I'll just leave that part out.

I need data copied from A2:I2 then pasted into named range "blah" (B2,D2,F2,B4,D4,F4,B6,D6,F6)

Can someone point me in the correct direction please? It's actually way more complicated than this, but I can't seem to get this part working. Thanks for any insight.

Consider this as an example:

Sub MoveData()
    Dim r1 As Range, r2 As Range, r As Range, _
        indx As Long, ary()
    Set r1 = Range("A2:I2")
    Set r2 = Range("B2,D2,F2,B4,D4,F4,B6,D6,F6")
    r2.Name = "blah"

    ary = r1

    indx = 1
    For Each r In Range("blah")
        r.Value = ary(1, indx)
        indx = indx + 1
    Next r
End Sub

There are multiple ways of referencing cells in excel cell these are relative or absolute or semi-absolute. Have you tried using another style.

Or

can you use an offset function like this in the next column =OFFSET($a2,0,2*(row()-1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM