[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=blue]Sub[/color] aryansaran2008SimpleSort()
[color=lightgreen]'.1) Some initial Workbook info: Summary sheet name and number of sheets[/color]
[color=blue]Dim[/color] wsSummary [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wsSummary = ThisWorkbook.Worksheets("May Forecast") [color=lightgreen]'Abbreviation gets methods, Properties of worksheets object through .dot[/color]
[color=blue]Dim[/color] wsCnt [color=blue]As[/color] [color=blue]Long[/color], Cnt [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'variables for number of worksheeets. Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster.[/color]
[color=blue]Let[/color] wsCnt = ThisWorkbook.Worksheets.Count [color=lightgreen]'Obtain number of worksheets inn this workbook[/color]
[color=lightgreen]'.2) Clear Summary sheet and Put all headings in[/color]
wsSummary.UsedRange.ClearContents [color=lightgreen]'Will clear anything on summary sheet ( Any ranges that were used )[/color]
wsSummary.Range("A1").Resize(1, 2) = Array("2015 022838 Forecast with I&O Costs", "")
wsSummary.Range("A2").Resize(1, 2) = Array("name", "amount")
wsSummary.Range("A3").Resize(1, 2) = Array("2015 External Forecast", "")
wsSummary.Range("A4").Resize(1, 2) = Array("name", "amount")
wsSummary.Range("A5").Resize(1, 2) = Array("2015 ADMIN Forecast", "")
wsSummary.Range("A6").Resize(1, 2) = Array("name", "amount")
wsSummary.Range("A7").Resize(1, 2) = Array("2015 022838 Forecast without I&O", "")
wsSummary.Range("A8").Resize(1, 2) = Array("name", "amount")
[color=lightgreen]'.3) Main loopings for bringing info from shhets to Summary Sheet[/color]
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color], r2 [color=blue]As[/color] [color=blue]Long[/color], rInsert [color=blue]As[/color] [color=blue]Long[/color], rHd [color=blue]As[/color] [color=blue]Long[/color], lr [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'variable for rows in sheets, and last Row of sheet. Assume our File for Input has a reasonably well defined end.[/color]
[color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet [color=lightgreen]'Worksheet being looked at in loop below[/color]
[color=blue]Dim[/color] namemax [color=blue]As[/color] Long: [color=blue]Let[/color] namemax = 5 [color=lightgreen]'Maximum likely number of names in any heading[/color]
[color=lightgreen]'.3a) Loop for each sheet[/color]
[color=blue]For[/color] Cnt = wsCnt [color=blue]To[/color] 1 [color=blue]Step[/color] -1 [color=lightgreen]'Take ( Loop) each worksheet in turn ( Working "backwards" is simply done to get the name order required )[/color]
[color=blue]Set[/color] ws = ThisWorkbook.Worksheets.Item(Cnt)
[color=blue]If[/color] ws.Name <> wsSummary.Name And ws.Name <> "SheetToIgnoor" [color=blue]Then[/color] [color=lightgreen]'Disclude any sheets to be discluded[/color]
[color=lightgreen]'.3b)Outer Loop of rows within each sheet[/color]
[color=blue]Let[/color] lr = ws.Cells(Rows.Count, 1).End(xlUp).Row [color=lightgreen]'The last cell in column 1 has the .End property( Argument "gooing upwards" applied returning a new range (cell) from which the .Row Property returs the last row.[/color]
[color=blue]For[/color] r = 1 [color=blue]To[/color] lr [color=blue]Step[/color] 1 [color=lightgreen]'Main loop for each sheet, going down rows[/color]
[color=blue]If[/color] Left(ws.Cells(r, 1).Value, 4) = "2015" [color=blue]Then[/color] [color=lightgreen]' we hit a header, so[/color]
[color=lightgreen]'.3c) Nested inner loop for case more than one name....[/color]
[color=blue]For[/color] r2 = r + 2 [color=blue]To[/color] r + 2 + namemax - 2 [color=lightgreen]'..... - Go down names after heading[/color]
[color=blue]If[/color] Left(ws.Cells(r2, 1).Value, 4) <> "2015" [color=blue]Then[/color] [color=lightgreen]'Condition met for name[/color]
[color=lightgreen]'If ws.Cells(r2, 1).Value <> "" And Left(ws.Cells(r2, 1).Value, 4) <> "2015" Then 'This alternative line to above line would not include empty cells[/color]
[color=blue]Let[/color] rInsert = rInsert + 1 [color=lightgreen]'Increment for insert row based on number of names in nested loop[/color]
[color=blue]Let[/color] rHd = wsSummary.Cells.Find(What:=ws.Cells(r, 1).Value, After:=wsSummary.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row [color=lightgreen]'Determine Row in Summary sheet with current heading being investigated in current sheet. Method: You start at first cell then go forwards (which effectively starts at end of sheet), sercching for anything ( = * ) by rows, then get the row number. This allows for different excel versions with different available Row numbers) Just a different method here for fun- finds last row in sheet rather than row for last entry in particular cell[/color]
ws.Rows(r2).Copy [color=lightgreen]'Copy appropriate line in current sheet being looked at and...[/color]
wsSummary.Rows(rHd + 2 + rInsert - 1).Insert Shift:=xlDown
[color=blue]If[/color] r = lr - 2 [color=blue]Then[/color] [color=blue]Exit[/color] [color=blue]For[/color] [color=lightgreen]'This line prevents copying empty lines after the last line in the current sheet[/color]
[color=blue]Else[/color] [color=lightgreen]'we have A heading (Or empty cell ) so[/color]
[color=blue]Exit[/color] [color=blue]For[/color] [color=lightgreen]'exit this nested loop and go back to main loop in each sheet[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r2 [color=lightgreen]'Go and check for another name under current heading[/color]
[color=blue]Let[/color] rInsert = 0 [color=lightgreen]'reset the row for number of names within nested loop[/color]
[color=blue]Else[/color] [color=lightgreen]'No header, no action, redundant code[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r [color=lightgreen]'Go to next row in current sheet being looked at[/color]
[color=blue]Else[/color] [color=lightgreen]'Do nothing. Redundant code[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] Cnt [color=lightgreen]'go to next worksheet[/color]
Application.CutCopyMode = [color=blue]False[/color] [color=lightgreen]'Stops screen selection flicker after Pasting[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'aryansaran2008SimpleSort()[/color]