Skipping Elements in an Array.

vbsjive

New Member
Joined
Mar 2, 2011
Messages
7
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I am trying to accomplish combining elements from one range to that of another range and placing the result into one cell.<o:p></o:p>
<o:p></o:p>
Here is the code im working with:<o:p></o:p>
<o:p></o:p>
Sub CommandButton6kl4_Click()<o:p></o:p>
Dim Rng1 As Range<o:p></o:p>
Dim Rng2 As Range<o:p></o:p>
Dim ray(1 To 10, 1 To 2359)<o:p></o:p>
Dim Rw As Integer<o:p></o:p>
Dim AC As Integer<o:p></o:p>
Set Rng1 = Range("b1:b10")<o:p></o:p>
Set Rng2 = Range("A441:j499")<o:p></o:p>
For Rw = 1 To 10<o:p></o:p>
For AC = 1 To 2359<o:p></o:p>
ray(Rw, AC) = IIf(Rng1.Cells(1, Rw) = 0 Or Rng2.Cells(1, AC) = 0, 0, IIf(Rng1.Cells(1, Rw) > Rng2.Cells(1, AC) Or Rng1.Cells(1, Rw) = Rng2.Cells(1, AC), 0, Rng1.Cells(1, Rw) & Rng2.Cells(1, AC)))<o:p></o:p>
Next AC<o:p></o:p>
Next Rw<o:p></o:p>
Range("K2705:T5064") = ray<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
There are lots of range values I am working with that I won't need as they contain Zeros. I want a function that skips over all Zeros in each Range, so that all combinations of each range are realized. <o:p></o:p>
<o:p></o:p>
So Rng1 as defined above has 10 possible elements and Rng2 (is an area) has 3481 elements.<o:p></o:p>
<o:p></o:p>
If Rng1 is say: 0,3,40,565,965,0,0,1,2,16<o:p></o:p>
<o:p></o:p>
and Rng2 is say: 21,0,4,0,0,0,0,0,etc,etc,etc<o:p></o:p>
<o:p></o:p>
I want the output to be: 321,34,3etc,3etc until the routine has assigned each possible combination a place in the array.<o:p></o:p>
<o:p></o:p>
It’s crucial that only elements greater than zero are combined and that no elements are output in their original state (no values in either range can combine with 0)<o:p></o:p>
<o:p></o:p>
Also Rng1 is defined as linear column where as Rng2 is an area. Not sure if this screws it up at runtime.<o:p></o:p>
<o:p></o:p>
Running this a couple of times I often end up with the #N/A output, with the correct array size and a bunch of zeros I don't want.<o:p></o:p>
<o:p></o:p>
Also, you might be wondering why the array has to be this big. The ranges themselves are dynamic so the position of the zero values change, thus changing how many elements the array could be filled with. It’s possible the array could be filled to its maximum capacity or with nothing at all.<o:p></o:p>
<o:p></o:p>
<o:p></o:p>

Stumped, Please help.<o:p></o:p>
<o:p></o:p>

Thanks!<o:p></o:p>
<o:p></o:p>
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi vbsjive,

Also, you might be wondering why the array has to be this big. The ranges themselves are dynamic so the position of the zero values change, thus changing how many elements the array could be filled with. It’s possible the array could be filled to its maximum capacity or with nothing at all.

You might consider using one or more dynamic arrays instead of a static array that is sized for your worst case scenario.

Perhaps you create a one-dimensional array for each range and populate them with just the greater than zero values. Then you could use two For...next statements to make all the combinations.

That would be more efficient since you would only have to remove the zero values once instead of repeating that process for each value in Rng2 each time you step to your next Rng1 value.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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