Getting a bit complicated for a forum like this but see how this goes.
<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Locations()<br> <SPAN style="color:#00007F">Dim</SPAN> Stores <SPAN style="color:#00007F">As</SPAN> Range, Store <SPAN style="color:#00007F">As</SPAN> Range<br> <SPAN style="color:#00007F">Dim</SPAN> Num <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, TopRws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, LR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> TargetRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, TargetCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> otr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, etr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, obr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, ebr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br> <SPAN style="color:#00007F">Const</SPAN> AllocCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 8 <SPAN style="color:#007F00">'<- 1st Allocation column</SPAN><br> <SPAN style="color:#00007F">Const</SPAN> HdrRw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 1 <SPAN style="color:#007F00">'<- Allocation header row</SPAN><br> <SPAN style="color:#00007F">Const</SPAN> SeqChange <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 487 <SPAN style="color:#007F00">'<- Where seq change occurs</SPAN><br> <br> Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br> TopRws = Int(SeqChange / 2)<br> <SPAN style="color:#00007F">Set</SPAN> Stores = Range("A3", Range("A3").End(xlDown))<br> <SPAN style="color:#00007F">With</SPAN> Cells(HdrRw, AllocCol - 2).Resize(, 3)<br> .Value = Array("Sequence", "Loc Num", "Allocation")<br> .Offset(, 3).Value = .Value<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br> <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Store <SPAN style="color:#00007F">In</SPAN> Stores<br> <br> <SPAN style="color:#00007F">If</SPAN> Cells(HdrRw + TopRws, AllocCol).Value = "" <SPAN style="color:#00007F">Then</SPAN><br> otr = Cells(HdrRw + TopRws, AllocCol).End(xlUp).Row<br> <SPAN style="color:#00007F">Else</SPAN><br> otr = HdrRw + TopRws<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">If</SPAN> Cells(HdrRw + TopRws, AllocCol + 3).Value = "" <SPAN style="color:#00007F">Then</SPAN><br> etr = Cells(HdrRw + TopRws, AllocCol + 3).End(xlUp).Row<br> <SPAN style="color:#00007F">Else</SPAN><br> etr = HdrRw + TopRws<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">If</SPAN> Cells(HdrRw + TopRws + 1, AllocCol).Value = "" <SPAN style="color:#00007F">Then</SPAN><br> obr = HdrRw + TopRws<br> <SPAN style="color:#00007F">Else</SPAN><br> obr = Cells(Rows.Count, AllocCol).End(xlUp).Row<br> End <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">If</SPAN> Cells(HdrRw + TopRws + 1, AllocCol + 3).Value = "" <SPAN style="color:#00007F">Then</SPAN><br> ebr = HdrRw + TopRws<br> <SPAN style="color:#00007F">Else</SPAN><br> ebr = Cells(Rows.Count, AllocCol + 3).<SPAN style="color:#00007F">End</SPAN>(xlUp).Row<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <br> <SPAN style="color:#00007F">With</SPAN> Store<br> Num = .Offset(, 1).Value<br> <SPAN style="color:#00007F">If</SPAN> etr < otr And etr + Num <= HdrRw + TopRws <SPAN style="color:#00007F">Then</SPAN><br> TargetRow = etr + 1<br> TargetCol = AllocCol + 3<br> <SPAN style="color:#00007F">ElseIf</SPAN> otr + Num <= HdrRw + TopRws <SPAN style="color:#00007F">Then</SPAN><br> TargetRow = otr + 1<br> TargetCol = AllocCol<br> <SPAN style="color:#00007F">ElseIf</SPAN> ebr < obr <SPAN style="color:#00007F">Then</SPAN><br> TargetRow = ebr + 1<br> TargetCol = AllocCol + 3<br> Num = .Offset(, 3).Value<br> <SPAN style="color:#00007F">Else</SPAN><br> TargetRow = obr + 1<br> TargetCol = AllocCol<br> Num = .Offset(, 2).Value<br> End <SPAN style="color:#00007F">If</SPAN><br> <br> Cells(TargetRow, TargetCol).Resize(Num).Value = Store.Value<br> <br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br> <SPAN style="color:#00007F">Next</SPAN> Store<br> <br> LR = Cells(Rows.Count, AllocCol).End(xlUp).Row<br> <SPAN style="color:#00007F">With</SPAN> Cells(HdrRw + 1, AllocCol - 2).Resize(LR - HdrRw, 2)<br> .Cells(1, 1).Resize(, 2).Value = Array(1, 11)<br> .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=2<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br> <br> LR = Cells(Rows.Count, AllocCol + 3).<SPAN style="color:#00007F">End</SPAN>(xlUp).Row<br> <SPAN style="color:#00007F">With</SPAN> Cells(HdrRw + 1, AllocCol + 1).Resize(LR - HdrRw, 2)<br> .Cells(1, 1).Resize(, 2).Value = Array(2, 12)<br> .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=2<br> End <SPAN style="color:#00007F">With</SPAN><br> Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>End <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
Store and location info for where the change occurs:
Excel Workbook |
---|
|
---|
| A | B | C | D |
---|
391 | a366 | 3 | 4 | 8 |
---|
392 | a367 | 1 | 1 | 1 |
---|
393 | a368 | 1 | 1 | 2 |
---|
394 | a369 | 1 | 9 | 5 |
---|
395 | a370 | 1 | 1 | 1 |
---|
396 | a371 | 1 | 1 | 2 |
---|
397 | a372 | 1 | 1 | 2 |
---|
398 | a373 | 5 | 8 | 2 |
---|
399 | a374 | 1 | 1 | 1 |
---|
400 | a375 | 1 | 2 | 3 |
---|
401 | a376 | 1 | 2 | 3 |
---|
402 | a377 | 1 | 1 | 2 |
---|
403 | a378 | 1 | 1 | 2 |
---|
404 | a379 | 2 | 1 | 2 |
---|
405 | a380 | 3 | 4 | 8 |
---|
406 | a381 | 1 | 1 | 2 |
---|
407 | a382 | 1 | 1 | 1 |
---|
408 | a383 | 1 | 2 | 2 |
---|
409 | a384 | 1 | 2 | 3 |
---|
410 | a385 | 1 | 1 | 1 |
---|
|
---|
Allocations made by the above code for this info:
Excel Workbook |
---|
|
---|
| F | G | H | I | J | K | L |
---|
236 | 469 | 479 | a364 | 470 | 480 | a363 | |
---|
237 | 471 | 481 | a365 | 472 | 482 | a366 | |
---|
238 | 473 | 483 | a367 | 474 | 484 | a366 | |
---|
239 | 475 | 485 | a368 | 476 | 486 | a366 | |
---|
240 | 477 | 487 | a369 | 478 | 488 | a370 | |
---|
241 | 479 | 489 | a371 | 480 | 490 | a372 | |
---|
242 | 481 | 491 | a374 | 482 | 492 | a375 | |
---|
243 | 483 | 493 | a376 | 484 | 494 | a377 | |
---|
244 | 485 | 495 | a378 | 486 | 496 | a381 | |
---|
245 | 487 | 497 | a373 | 488 | 498 | a379 | |
---|
246 | 489 | 499 | a373 | 490 | 500 | a379 | |
---|
247 | 491 | 501 | a373 | 492 | 502 | a380 | |
---|
248 | 493 | 503 | a373 | 494 | 504 | a380 | |
---|
249 | 495 | 505 | a373 | 496 | 506 | a380 | |
---|
250 | 497 | 507 | a373 | 498 | 508 | a380 | |
---|
251 | 499 | 509 | a373 | 500 | 510 | a380 | |
---|
252 | 501 | 511 | a373 | 502 | 512 | a380 | |
---|
253 | 503 | 513 | a382 | 504 | 514 | a380 | |
---|
254 | 505 | 515 | a383 | 506 | 516 | a380 | |
---|
255 | 507 | 517 | a383 | 508 | 518 | a384 | |
---|
256 | 509 | 519 | a385 | 510 | 520 | a384 | |
---|
257 | 511 | 521 | a386 | 512 | 522 | a384 | |
---|
258 | 513 | 523 | a386 | 514 | 524 | a387 | |
---|
|
---|