UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Alternative To Looping Through Array?, Office 2007    
 
   
kruuth
post Feb 19 2012, 07:28 PM
Post #1

UtterAccess Guru
Posts: 625



This is kind of a strange problem I'm having. I was hoping someone could elaborate on it.

I have a 2d array of numbers, lists 1-4. Right now when I loop I get something like this:

1
2
3
4
1
2
3
4
1
2
3
4
1
2
3

if I loop 15 times. Would it be possible to somehow get an output like this:
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4

I'm still a bit green on vba loops.
Go to the top of the page
 
+
briangriffey
post Feb 19 2012, 11:09 PM
Post #2

UtterAccess Veteran
Posts: 312
From: Las Cruces, NM



You would need two loops... nested.
Go to the top of the page
 
+
BruceM
post Feb 20 2012, 07:19 AM
Post #3

UtterAccess VIP
Posts: 2,444
From: Downeast Maine



Edit: I just noticed that this is an Excel question. My questions remain, but I would not have replied had I noticed in time that it is posted in the Excel forum.

Original reply: How is the list generated? Perhaps the order could be imposed there instead of during the loop?

This post has been edited by BruceM: Feb 20 2012, 07:22 AM
Go to the top of the page
 
+
kruuth
post Feb 20 2012, 07:55 AM
Post #4

UtterAccess Guru
Posts: 625



This is a VBA in excel question.

I suppose that it could be done somehow onscreen if that was an option using excel to sort or something like that.
Go to the top of the page
 
+
Bob G
post Feb 20 2012, 08:08 AM
Post #5

UtterAccess VIP
Posts: 8,104
From: CT



are you saying you want to have the numbers 1 - 4 generated 15 times each but sorted all 1s then 2s..... instead of 1-4 15 times ??
Go to the top of the page
 
+
kruuth
post Feb 20 2012, 08:45 AM
Post #6

UtterAccess Guru
Posts: 625



Yeah, pretty much.
Go to the top of the page
 
+
bulsatar
post Feb 20 2012, 09:05 AM
Post #7

UtterAccess Ruler
Posts: 1,175
From: Indiana, USA



Yep, I agree with Brian, 2 loops nested.

example:
CODE
for i = 1 to 4
     for a = 1 to 4
          debug.print i
     next a
next i
Go to the top of the page
 
+
Bob G
post Feb 20 2012, 09:17 AM
Post #8

UtterAccess VIP
Posts: 8,104
From: CT



a sample based on what everyone has contributed..

CODE
Public Sub testme()
Dim i As Integer
Dim a As Integer

For i = 1 To 4
     For a = 1 To 15
     ActiveCell.Value = i
     ActiveCell.Offset(1, 0).Activate
     Next a
Next i
End Sub
Go to the top of the page
 
+
StuKiel
post Feb 22 2012, 03:53 AM
Post #9

UtterAccess Ruler
Posts: 1,182
From: Norfolk UK



Hi,

Actually with some maths this can be achieved with just one loop:

CODE
Sub listNums()
Dim intRef as Integer
For intRef=1 to 15
    Cells(intRef,1)=INT((intRef-1)/4)+1
Next
End Sub


You can also achieve the same result using functions:

=INT((ROW()-1)/4)+1

Type this into the first row and then copy down to row 15.

HTH
Stu
Go to the top of the page
 
+
kruuth
post Mar 1 2012, 12:40 PM
Post #10

UtterAccess Guru
Posts: 625



Cool but I need to do this in VBA
Go to the top of the page
 
+
kruuth
post Mar 15 2012, 09:42 AM
Post #11

UtterAccess Guru
Posts: 625



Hey folks. I decided to revisit this since I have hit another little snag.... How would I do this with multiple arrays with different precedence? In this case I have a series of arrays:

1
2
3

4
5
6
7
8

9
10
11

and if I want to set it up to fill out based on the first, then second, then third so if I had a need for 15 it would look like this:

1,1,2,2,3,3,4,4,5,6,7,8,9,10,11

I've been beating my head against a wall figuring this out
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th May 2013 - 04:10 AM