View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default Every 5 digit combination

I believe this will do what you want - allows entries like 11111 as well as
12345, plus you can easily see how the 'move' to next row is made.

There are two ways to move around on an Excel sheet - 'physically' by using
a statement similar to:
ActiveCell.Offset(1,0).Activate
which says move down 1 row from where ever the current active cell is. But
if you set up a variable to use as a pointer or row counter, you can move
'logically' using it - the ActiveCell remains in one location and you work in
cells relative to it as
RowOffset=23
Range("A1").Select
ActiveCell.Offset(RowOffset,0) = "23 Rows Below the Active Cell"
Use RowOffset of 0 to put something on the same row with the ActiveCell.

Using the logical movement is much faster than actually physically moving
from cell to cell using the .Activate method.

Sub NumericCombinations()
Dim NumberSet(1 To 5) As Integer
Dim RowOffset As Long ' pointer to rows
Dim LC1 As Integer ' LoopCounter
Dim LC2 As Integer ' LoopCounter
Dim LC3 As Integer ' LoopCounter
Dim LC4 As Integer ' LoopCounter
Dim LC5 As Integer ' LoopCounter

'load the numbers into the array
'works for this special case of odd numbers
For LC1 = 0 To 4
NumberSet(LC1 + 1) = (LC1 * 2) + 1
Next
'
'RowOffset 'moves' you down thru the rows logically
'not an actual physical move - faster this way
RowOffset = 0
Range("A1").Select ' got to A1 on current active sheet
For LC1 = LBound(NumberSet) To UBound(NumberSet)
For LC2 = LBound(NumberSet) To UBound(NumberSet)
For LC3 = LBound(NumberSet) To UBound(NumberSet)
For LC4 = LBound(NumberSet) To UBound(NumberSet)
For LC5 = LBound(NumberSet) To UBound(NumberSet)
'put each number in a separate cell
ActiveCell.Offset(RowOffset, 0) = NumberSet(LC1)
ActiveCell.Offset(RowOffset, 1) = NumberSet(LC2)
ActiveCell.Offset(RowOffset, 2) = NumberSet(LC3)
ActiveCell.Offset(RowOffset, 3) = NumberSet(LC4)
ActiveCell.Offset(RowOffset, 4) = NumberSet(LC5)
'update pointer to logically move to next row:
RowOffset = RowOffset + 1
'to actually physically move to next row:
'Activecell.Offset(1,0).activate
'if you use this method, then the number entry
'above be done by substituting 0, for RowOffset, in
'each of the instructions placing a value in the cells
'example:
'ActiveCell.Offset(0, 0) = NumberSet(LC1)
'
Next ' LC5
Next ' LC4
Next ' LC3
Next ' LC2
Next ' LC1

End Sub


"seagee69" wrote:

As a newby to Excel VBA, I'd like to create a spreadsheet that would
give me every 5-digit combination of the numbers {1, 3, 5, 7, 9} in one
spreadsheet. The numbers can repeat.

I thought of the FOR NEXT loop, but I am not sure how to get the output
to the next row, nor am I certain it would give every combination.

Thanks so much,

seagee69