Once again my thanks. In the meantime I have been trying to solve the algorithm for mysef - the only way to learn!
Everything has worked as per the code below. However, there are a few things not going quite according to plan which it would be helpful if you could plesase look into. Firstly when the macro has run I seem to have "lost" all the menu ribbons. Sometimes the sheet has lost is auto calcualtion.
I'm tring to pass the numeric value from my numeric TempArray() as a character to my RealArray(x,y) but I'm getting a subscript error
Finally I thought I would be able to grab the values in cells from the worksheet and assign them to my vba variables. This works OK for anything other than a cell which contains a calculated value.
I have inserted the code into a new module - is that the correct way?
If you try and run the code you'll se what I mean. You'll need to put values in cells A2, B3 and B4 Suggest 56, 5 and 4 respectively.
I'm sure that once these few isues are ironed out I'll be able to continue unaided with my project! Thanks again. DJH
Option Base 1
Sub macro1()
Optimise (False)
MyDir = MyPath = ActiveWorkbook.Path
Dim Combinations As Integer
MyRow = 5
ColOffset = 10
CombCount = 0
Layouts = Cells(4, 2).Value
Panels = Cells(3, 2).Value
Combinations = Cells(2, 2).Value
Column = Panels
Columns("K:BB").Clear
ReDim TempArray(Panels)
ReDim RealArray(Combinations, Panels)
'Initiate first Combination to A,A,A,A,A, etc., and set Temporary Panel/Col values all to 1
For Count = 1 To Panels
TempArray(Count) = 1
RealArray(1, Count) = Chr(1 + 64)
Next
'--------------------------------------------------------------------
'Display first combination
Range(Cells(MyRow, ColOffset), Cells(MyRow, (Panels + ColOffset - 1))).Value = TempArray
'Display = Display + 1
'----------------------------------------------------------------------
'Main Loop and Routine exit condition
Loop1:
If CombCount > Panels Then GoTo Quit
MyRow = MyRow + 1
'Display = Display + 1
'Loop round incrementing value in final Panel/Column position by 1
For Count = 1 To Panels
If Count = Column Then
TempArray(Count) = TempArray(Count) + 1
End If
RealArray(MyRow, Count) = Chr(TempArray(Count) + 64) 'Changes Layout Number to Letter "A", "B" etc
Next
Range(Cells(MyRow, ColOffset), Cells(MyRow, (Panels + ColOffset - 1))).Value = TempArray 'Displays Combinations row by row with Layout Number
'when final Column contains the last layout value.......
If TempArray(Panels) = Layouts Then
CombCount = 0
'Count how many Panels/Columns contain the last Layout value
For Temp1 = Panels To 1 Step -1
If TempArray(Temp1) = Layouts Then
CombCount = CombCount + 1
End If
Next
'-------------------------------------
If CombCount = Panels Then GoTo Quit 'If all Panels/Columns contain the last layout Value then the Routine is complete
TempArray(Panels - CombCount) = TempArray(Panels - CombCount) + 1 'Set the value of the Panel/Column immediately preceeding
'the column in which the value= last Layout value
'to 1 + the value of that coulmn in thepreceeding Row
'backtrack along the Panels/Columns and replace the TempArray(Panel/Column) with the value calualted above
For Temp2 = (Panels - CombCount) To (Panels - 1)
TempArray(Temp2) = TempArray(Panels - CombCount)
Next
TempArray(Panels) = TempArray(Panels - CombCount) - 1 'set the value of the final Panel/Column to value calculated above
'but deduct 1 as its value is incremented when the Loopbegins again
End If
GoTo Loop1
Quit:
Optimise (True)
End Sub
Sub Optimise(Flag As Boolean)
On Error Resume Next
Application.ScreenUpdating = Flag
Application.DisplayAlerts = Flag
Application.EnableEvents = Flag
Application.DisplayStatusBar = Flag
ActiveSheet.DisplayPageBreaks = Flag
If Flag = False Then
Application.Calculation = xlCalculationAutomatic
Else
Application.Calculation = xlCalculationManual
End If
On Error GoTo 0
End Sub
Bookmarks