I think my syntax is just wrong somewhere but maybe I'm just missing an easier solution. Here are the blocks of code... I don't think the sheet will help so much but I can post if necessary.
Basically the first piece of code is looping through columns checking to see if the column total (TotalAllocated) matches the target total (TotalSold). If it doesn't match (TotalAllocated should always be<= TotalSold), then 1 is added to the total and the code steps over to the next column. If I run out of columns then it loops back to the first column with data (c=3). This seems to be working just fine but I want to make sure my For and If statements are done correctly. I get the feeling I should be including End If statements but when I add them I start getting Block errors. Is it ok to leave the code as is, without the End If? I understand that some single-line If statements are ok to use, while others are not, but I've yet to find a clear explanation of these "rules".
The next piece of code is somewhat similar...I need to loop through all rows, checking to see if the row total (RowAllocated) matches the target (RowSold). If the row values match then I need to proceed to the next row. If they don't match then I need to check if the column totals have not yet maxed out, and if so, add 1 to the current row,column. If the column is maxed out then I need to step through to the next column of the row until i find one that can take the +1. This loop continues through each row until all rows are full. My code below seems to get stuck in an infinite loop once I hit the last row (it just keeps looping from the first to the last column).
The sheet below shows the code about halfway through, so you can see some rows/columns completed with others still to go.
Can anyone offer some help? Much appreciated in advance...
Basically the first piece of code is looping through columns checking to see if the column total (TotalAllocated) matches the target total (TotalSold). If it doesn't match (TotalAllocated should always be<= TotalSold), then 1 is added to the total and the code steps over to the next column. If I run out of columns then it loops back to the first column with data (c=3). This seems to be working just fine but I want to make sure my For and If statements are done correctly. I get the feeling I should be including End If statements but when I add them I start getting Block errors. Is it ok to leave the code as is, without the End If? I understand that some single-line If statements are ok to use, while others are not, but I've yet to find a clear explanation of these "rules".
Code:
'Loop to check that TotalAllocated = TotalSold
'If TotalAllocated< TotalSold, 1 share will be added, one client at a time from largest order to smallest
'until TotalAllocated = TotalSold
For c = 3 To LastCol
If TotalAllocated = TotalSold Then Exit For
Cells(4, c).Value = Cells(4, c).Value + 1
TotalAllocated = Application.Sum(Range(Cells(4, 3), Cells(4, LastCol)))
Cells(4, LastCol + 1).Value = TotalAllocated
If c = LastCol Then c = 2
Next c
The next piece of code is somewhat similar...I need to loop through all rows, checking to see if the row total (RowAllocated) matches the target (RowSold). If the row values match then I need to proceed to the next row. If they don't match then I need to check if the column totals have not yet maxed out, and if so, add 1 to the current row,column. If the column is maxed out then I need to step through to the next column of the row until i find one that can take the +1. This loop continues through each row until all rows are full. My code below seems to get stuck in an infinite loop once I hit the last row (it just keeps looping from the first to the last column).
The sheet below shows the code about halfway through, so you can see some rows/columns completed with others still to go.
Can anyone offer some help? Much appreciated in advance...
Code:
For r = 7 To LastRow
RowSold = Cells(r, 1)
RowAllocated = Cells(r, LastCol + 1)
If RowSold > RowAllocated Then
For c = 3 To LastCol
If Cells(LastRow + 1, c)< Cells(4, c) Then
If RowSold = RowAllocated Then Exit For
Cells(r, c).Value = Cells(r, c).Value + 1
RowAllocated = Application.Sum(Range(Cells(r, 3), Cells(r, LastCol)))
Cells(r, LastCol + 1).Value = RowAllocated
Cells(LastRow + 1, c).Value = Application.Sum(Range(Cells(7, c), Cells(LastRow, c)))
If c = LastCol Then c = 2
Else
If c = LastCol Then c = 2
End If
Next c
Next r
Allocation Sheet - Loop Version.xls | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | ClientName | Purple | Grey | White | Totals | |||
2 | OrderQty | 10,288 | 9,571 | 5,508 | 25,367 | |||
3 | %ofTotalOrder | 40.5566% | 37.7301% | 21.7132% | 100.0000% | |||
4 | SharestobeAllocated | 1,096 | 1,019 | 586 | 2,701 | |||
5 | ||||||||
6 | Quantity | Price | ||||||
7 | 300 | 25.89 | 122 | 113 | 65 | 300 | ||
8 | 300 | 25.895 | 122 | 113 | 65 | 300 | ||
9 | 100 | 26.365 | 41 | 38 | 21 | 100 | ||
10 | 200 | 26.38 | 82 | 75 | 43 | 200 | ||
11 | 200 | 26.39 | 82 | 75 | 43 | 200 | ||
12 | 500 | 26.4 | 202 | 189 | 108 | 499 | ||
13 | 200 | 26.41 | 81 | 75 | 43 | 199 | ||
14 | 200 | 26.42 | 81 | 75 | 43 | 199 | ||
15 | 200 | 26.46 | 81 | 75 | 43 | 199 | ||
16 | 100 | 26.48 | 40 | 37 | 21 | 98 | ||
17 | 200 | 26.55 | 81 | 75 | 43 | 199 | ||
18 | 80 | 26.56 | 32 | 30 | 17 | 79 | ||
19 | 121 | 26.57 | 49 | 45 | 26 | 120 | ||
20 | TotalSold | 2,701 | 1,096 | 1,015 | 581 | 2,685 | ||
21 | AveragePrice | 26.3168 | 26.3170 | 26.3163 | 26.3159 | 26.3165 | ||
Sheet1 |