Write to a non contiguous range with VBA

Markus71

New Member
Joined
May 30, 2021
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Dear Users,

I am almost new to VB and I have been out there every night, since days, exploring the web to find a solution to write a range of horzizontal data to a multiple range of non contiguous cells.
After a while I had figured out, how to fill an array with the sourcerange, but pasting to the destinationrange which consists of contigous cells resulted always in the situation, that only the first entry of the array showed up in the destination range. Now, I have found a thread here, which helped me to construct a solution. The thread was >2000days old and the board recommended to start a new threat. Here I am.
Having said that, the solution I have now, works somehow, but it is really slow, because of the 2 loops I am using.

Basically I have a row of horizontal data with 344 cells. Due to the limitations in Range length I have splitted the range in 4x86 cells.
I take the 86 values in an array and then loop through the destination range.
I would appreciate if some pro can take a glance at it and provide me some support to make it faster.
Excel Formula:
Sub Test_Range3()
    Dim Sourcerng, Destinationrng As Range
    Dim rCell, acell As Range
    Dim i, n As Long

    Application.ScreenUpdating = False
    Set Sourcerng = Sheets("Database").Range("A1:CH1")
     n = Sourcerng.Cells.Count
        ReDim MyAr(1 To n)
        n = 1
        For Each acell In Sourcerng
            MyAr(n) = acell.Value
            n = n + 1
        Next acell
    
    i = 1
    Set Destinationrng = Sheets("Database").Range("L12:L14,L16:L20,L22:L26,L28:L30,L34:L35,M13,M16:M20,M29:M30,N13:N14,N16:N19,N22:N26,O13:O14,O16:O19,Q12:Q14,Q16:Q20,Q22:Q26,Q28:Q30,Q34:Q35,R13,R16:R20,R29:R30,S13:S14,S16:S19,S22:S26,T13:T14,T16:T19")
    For Each rCell In Destinationrng
        rCell.Value = MyAr(i)
       i = i + 1
    Next rCell
Application.ScreenUpdating = True
End Sub
 
Let's try to clarify what the second code is supposed to do.
My understanding was that it should take whatever data is in row 1 of 'Database' and put those values into that non-contiguous range as follows
A1 goes to L12
A2 -> L13
A3 -> L14
A4 -> L16
.
.
.
Q1 -> L34
R1 -> L35
S1 -> M13
T1 -> M16
etc

If that is not correct, please clarify.
Sorry, again overlapping Peter ...
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Akuinis approach works and I'll give it a try, because I assume all my confusion is somehow related to the union function and the order of the elements within.
If I can avoid it, it will be somehow easier for me to understand and I do not have to take care of the areas.
It resolves the problem with 255 char limit & keep the range order intact.
You can uncomment this line:
Debug.Print r.Address
to see the order of the range address in the immediate window.
 
Upvote 0
Let's try to clarify what the second code is supposed to do.
My understanding was that it should take whatever data is in row 1 of 'Database' and put those values into that non-contiguous range as follows
A1 goes to L12
A2 -> L13
A3 -> L14
A4 -> L16
.
.
.
Q1 -> L34
R1 -> L35
S1 -> M13
T1 -> M16
etc

If that is not correct, please clarify.
Hello Peter, yes that is what it should do. I have pasted a Screenshot of my Database page. Row 1 contains the data which is pulled from the non contigious ranges.
Below are the pattern of the ranges, which do not work with Code2 from Peter but with the original code - but as I said, now in wrong order because of the areas, i guess ...
Capture.JPG
 
Upvote 0
It resolves the problem with 255 char limit & keep the range order intact.
You can uncomment this line:
Debug.Print r.Address
to see the order of the range address in the immediate window.
Yes Akuini, the character limit was the beginning of my problem, but with the union fuction, other problems arised, which we tried to tackle here
 
Upvote 0
yes that is what it should do.
That is what it does for me.
Here is my sheet before the Row_To_NonContiguous code has been run. The numbers in row 1 continue consecutively up to 172 in FP1. The colours are only there to help me visually identify the cells in the non-contiguous range.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Database


.. and here is the same sheet after running the Row_To_NonContiguous code.
As far as I understand it, that is exactly what you wanted. Sorry if it is not working at your end.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
1214487130
1321927384562708188105113124131148156167
143283946718289114125132157168
15
1642029404763728390106115126133149158169
1752130414864738491107116127134150159170
1862231424965748592108117128135151160171
1972332435066758693109118129136152161172
20824516794110137153
21
22933527695119138162
231034537796120139163
241135547897121140164
251236557998122141165
261337568099123142166
27
281457100143
2915255868101111144154
3016265969102112145155
31
32
33
341760103146
351861104147
36
Database
 
Upvote 0
That is what it does for me.
Here is my sheet before the Row_To_NonContiguous code has been run. The numbers in row 1 continue consecutively up to 172 in FP1. The colours are only there to help me visually identify the cells in the non-contiguous range.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Database


.. and here is the same sheet after running the Row_To_NonContiguous code.
As far as I understand it, that is exactly what you wanted. Sorry if it is not working at your end.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
1214487130
1321927384562708188105113124131148156167
143283946718289114125132157168
15
1642029404763728390106115126133149158169
1752130414864738491107116127134150159170
1862231424965748592108117128135151160171
1972332435066758693109118129136152161172
20824516794110137153
21
22933527695119138162
231034537796120139163
241135547897121140164
251236557998122141165
261337568099123142166
27
281457100143
2915255868101111144154
3016265969102112145155
31
32
33
341760103146
351861104147
36
Database
Thank you Peter, this is exactly what it should do. There must be a mistake from my side, but I have no idea where. I‘ll start from scratch when I‘m back on my PC.
Again I have to tilt my head for your superlative support in this case and the time you have spent for me.
 
Upvote 0
That is what it does for me.
Here is my sheet before the Row_To_NonContiguous code has been run. The numbers in row 1 continue consecutively up to 172 in FP1. The colours are only there to help me visually identify the cells in the non-contiguous range.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Database


.. and here is the same sheet after running the Row_To_NonContiguous code.
As far as I understand it, that is exactly what you wanted. Sorry if it is not working at your end.

Markus71.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
11234567891011121314151617181920212223242526272829303132
2
11
1214487130
1321927384562708188105113124131148156167
143283946718289114125132157168
15
1642029404763728390106115126133149158169
1752130414864738491107116127134150159170
1862231424965748592108117128135151160171
1972332435066758693109118129136152161172
20824516794110137153
21
22933527695119138162
231034537796120139163
241135547897121140164
251236557998122141165
261337568099123142166
27
281457100143
2915255868101111144154
3016265969102112145155
31
32
33
341760103146
351861104147
36
Database
Peter, just booted my PC and tried it again .... I am a Roooooooooooookie cause I pasted the code in the worksheet instead of the module ...
Your code works great!

Sorry for the confusion and Thank you for your time!!
Markus
 
Upvote 0
Ah, glad you got it sorted. Yes, better in a standard module. However, that issue has pointed out to me that the code does have a slight error.
Please change
VBA Code:
MyAr = Range("A1:FP1").Value2
to
Rich (BB code):
MyAr = .Range("A1:FP1").Value2
 
Upvote 0

Forum statistics

Threads
1,215,834
Messages
6,127,163
Members
449,368
Latest member
JayHo

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