[color=green]'[/color]
'
[color=darkblue]Sub[/color] KaranAdvFiltBodgeCopy2()
Application.ScreenUpdating = [color=darkblue]False[/color] [color=green]'Not necerssary but speeds things up a bit, by turning screen updating off.[/color]
[color=green]'On Error GoTo TheEnd 'If anything goes wrong go to the End instead of crashing.[/color]
[color=darkblue]Dim[/color] wks1 [color=darkblue]As[/color] Worksheet: [color=darkblue]Set[/color] wks1 = ThisWorkbook.Worksheets(1) [color=green]'Give abbreviation for first sheet in this all Properties and methoods of Object Worksheet[/color]
[color=green]'Start Bit to Delete Sheets / Tabs------------[/color]
Application.DisplayAlerts = [color=darkblue]False[/color] [color=green]'Prevents being asked everytime if you really want to delete the Workbook[/color]
[color=darkblue]Dim[/color] ws [color=darkblue]As[/color] Worksheet [color=green]'Tempory worksheet name. ws now has Methods and Properties of Worksheets obtained with typing . dot after it![/color]
[color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] ActiveWorkbook.Worksheets [color=green]'We could alternatively use something like ThisWorkbook, Workbooks("Itsname.xlxm")[/color]
[color=darkblue]If[/color] ws.Name <> wks1.Name [color=darkblue]Then[/color] [color=green]'Name property here returns name without .xlsm bit on end[/color]
ws.Delete
[color=darkblue]Else[/color] [color=green]'Presumably then the worksheet name is That of the first sheet so[/color]
[color=green]' do nothing (Don't delete it!)[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]Next[/color]
Application.DisplayAlerts = [color=darkblue]True[/color] [color=green]'Turn it back on[/color]
[color=green]'End Bit to delete any Sheets / Tabs------------[/color]
[color=green]'Add new Worksheets---[/color]
[color=green]'Make Tempory Sheet[/color]
[color=darkblue]Dim[/color] Record [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'Record name, not kept constant, used / updated in looping[/color]
[color=darkblue]Dim[/color] LastRecordRow [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]' Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) -Most smaller numbers, Byte, Integer, Single are converted in computer to long so no advantage of Dim to smaller Type here[/color]
[color=darkblue]Dim[/color] LastRecordColumn [color=darkblue]As[/color] [color=darkblue]Long[/color]
[color=green]'Let Worksheets.Add(After:=Worksheets(1)).Name = "Unique1" 'Add a Worksheet after the first, named Unique1 for now[/color]
[color=darkblue]Let[/color] Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Unique1" [color=green]'Add a Worksheet after the last, named Unique1 for now[/color]
[color=darkblue]Let[/color] LastRecordRow = wks1.Range("A" & Rows.Count).End(xlUp).Row [color=green]'Go to last row in Column 1, come back up to last entry and get the row there..allows for differnt versions of Excel with different number of rows.[/color]
[color=green]' Let LastRecordRow = wks1.Cells.Find(What:="*", After:=wks1.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Alternative method: You start at first cell then go backwards (which effectively starts at end of sheet. 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]
wks1.Range("A1:A" & LastRecordRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Unique1").Range("A1"), Unique:=[color=darkblue]True[/color] [color=green]'Copies entire A Column to first column in sheet2 (Tempory made "Unique1" sheet), The important bit is Unique:=True - that only copies unique Nuimbers[/color]
[color=green]'---------------------[/color]
[color=darkblue]Dim[/color] LastUnqRow [color=darkblue]As[/color] [color=darkblue]Long[/color], UqeRow [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]'Rows in Tempory Unique sheet. long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647)[/color]
[color=darkblue]Let[/color] LastUnqRow = Worksheets("Unique1").Cells(Rows.Count, 1).End(xlUp).Row [color=green]'get last Row from Column 1..[/color]
[color=green]' Let LastUnqRow = Worksheets("Unique1").Cells.Find(What:="*", After:=Worksheets("Unique1").Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Get last Unique Row for use in next loop. method: You starta at first cell then go backwards (which effectively starts at end of sheet. 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]
[color=green]'### get Row Indices for Stupid Bodge[/color]
[color=darkblue]Dim[/color] BodgeStartRow [color=darkblue]As[/color] Long: [color=darkblue]Let[/color] BodgeStartRow = 2 [color=green]'Start of first range for Re - Copy. Set tohere to just below heading[/color]
[color=darkblue]Dim[/color] RangeBodgeRows [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]'Variable for Range Row in New record Shhet[/color]
[color=darkblue]For[/color] UqeRow = 2 [color=darkblue]To[/color] LastUnqRow [color=darkblue]Step[/color] 1 [color=green]'[/color]
'Make new sheet------------
[color=darkblue]If[/color] Sheets("Unique1").Cells(UqeRow, 1).Text <> "" [color=darkblue]Then[/color] [color=green]'Assuming a Record is there[/color]
[color=darkblue]Let[/color] Record = Sheets("Unique1").Cells(UqeRow, 1).Text [color=green]'Put name in Record variable[/color]
[color=green]'Let Worksheets.Add(After:=Worksheets(1)).Name = Record 'Add new worksheet with Record name[/color]
[color=darkblue]Let[/color] Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Record
[color=green]'Copying data to new sheet----[/color]
wks1.UsedRange.AutoFilter Field:=1, Criteria1:=Record [color=green]'Filter out everything except with that with the appropriate Record (makes visible based on the criteria only the stuff you want)....[/color]
wks1.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets(Record).Range("A1") [color=green]', then combine it with SpecialCells to just copy that wot you see, (and then send it to the relavent new sheet , name n).. ( Idid notice that it works the same without the .SpecialCells(xlCellTypeVisible) bit, - but that mayjust be Excel “guessing wot you want” as it does, that is to say it copies by default wot is visible?- not too sure on that one yet.)[/color]
wks1.AutoFilterMode = [color=darkblue]False[/color] [color=green]'This has been automatically been set to true, so we only see the visible fltered. We could leave this turned on until the end, but for debugging it helps to turn it back onn here, that is to say make entire first sheet visible.[/color]
[color=green]'-------------------------------------------------[/color]
[color=darkblue]With[/color] Worksheets(Record).UsedRange [color=green]'Bit of simple Format Tidying up[/color]
.WrapText = [color=darkblue]False[/color]
.Columns.AutoFit
[color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]Else[/color]
[color=green]'Do nothing if no Record given[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=green]'-----------------------------[/color]
[color=green]'### Copy Bodge[/color]
[color=darkblue]Let[/color] LastRecordColumn = Worksheets(Record).Cells(2, Columns.Count).End(xlToLeft).Column [color=green]'Start in first Column second row, go to the last column in that row, come back to last entry and get the column number of it. Allows for differet Column numbers but for karan Column H would 'ave done!![/color]
[color=darkblue]Let[/color] RangeBodgeRows = Worksheets(Record).Range("A" & Rows.Count).End(xlUp).Row [color=green]'Needed range row length (+1) for copy bodge[/color]
[color=green]' Alternative for above for getting size of range to be copied Let RangeBodgeRows = Worksheets(Record).Range("A1").CurrentRegion.Rows.Count: Let LastRecordColumn = Worksheets(Record).Range("A1").CurrentRegion.Columns.Count[/color]
wks1.Range("A" & BodgeStartRow & ":A" & BodgeStartRow + RangeBodgeRows - 2).EntireRow.Copy Destination:=Sheets(Record).Range("A2") [color=green]'Copy destination method, but now using a specified Range[/color]
[color=darkblue]Let[/color] BodgeStartRow = BodgeStartRow + RangeBodgeRows - 1
[color=darkblue]Next[/color] UqeRow [color=green]'Go back and make another new sheet[/color]
[color=green]' ws.Range("A1:H1").Copy Destination:=Sheets(myarr(i) & "").Range("A1:H1")'Headers: - This is done alreadyas a by product from the Advanced filter copy[/color]
[color=green]'wks1.AutoFilterMode = False'It is more useual to do this here,[/color]
Application.DisplayAlerts = [color=darkblue]False[/color] [color=green]'Prevent being asked if you really want to delete Temporary Unique sheet[/color]
Sheets("Unique1").Delete [color=green]' delete the filtered Record name sheet as you do not need it any more[/color]
Application.DisplayAlerts = [color=darkblue]True[/color]
Application.ScreenUpdating = [color=darkblue]True[/color] [color=green]'Turn screen "back on" or screen is "dead"[/color]
[color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'We stop code here assuming it worked (or at least did not crash!)[/color]
TheEnd:
Application.ScreenUpdating = [color=darkblue]True[/color] [color=green]'Important to do this here so if anything goes wron then the screen updating is turned back on, ohterwisee the screen is dead[/color]
MsgBox (Err.Description) [color=green]'Print out error message in Message Box[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color] [color=green]'KaranAdvFiltBodgeCopy2()[/color]