VBA: Split data into multiple worksheets based on column

waxsublime

New Member
Joined
Jul 13, 2013
Messages
17
I'm trying to get this code I found (from How to split data into multiple worksheets based on column in Excel?) to work, but it's giving me an error.

Code:
Sub parse_data()
    Dim lr As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    vcol = 4
    Set ws = Sheets("Sheet1")
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    title = "A1:I1"
    titlerow = ws.Range(title).Cells(1).Row
    icol = ws.Columns.Count
    ws.Cells(1, icol) = "Unique"
    For i = 2 To lr
    On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
    ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
    Next
    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    ws.Columns(icol).Clear
    For i = 2 To UBound(myarr)
    ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
    Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
    ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
    Sheets(myarr(i) & "").Columns.AutoFit
    Next
    ws.AutoFilterMode = False
    ws.Activate
End Sub

Any ideas on how to fix this?

Thanks!
 
Last edited:
Hi Army man,
. ……..done a code for ya..

. ... I took the unusual (for me) starting point of actually writing a “UDF”, (User Defined Function), to do that initial bit of getting the shortened string you want to use as Filtering criteria and New Sheet Names.
.
. (I always prefer to keep everything in one code especially when I try to give it to anyone.. But I have to confess it starts getting a bit too big and messy. …. And I woz practicing these things just now… …….)

.
.
. FYI in case you don’t know. When you make a UDF you have a new “Home Made” Function. Let’s call it “UDArmyGeezerPickOutBit”.
.
. The nice thing is if you stick it in a Normal Module as a

Public Function UDArmyGeezerPickOutBit(LongStrings As Rang____________etc.
.
….. then you have it available both as a normal Spreadsheet Function and can use it in a VBA Code. Nice ‘cos you can check it works first in the Spread sheet.
.
. So get the UDF up and running before you do anything else..
.
. Copy and paste this in a normal Module.


Code:
[color=blue]Public[/color] [color=blue]Function[/color] UDArmyGeezerPickOutBit(LongStrings [color=blue]As[/color] Range, StartString [color=blue]As[/color] String, StopString [color=blue]As[/color] String)
 
    [color=blue]Dim[/color] vOut() [color=blue]As[/color] [color=blue]Variant[/color] [color=lightgreen]' Array but individual elements Has to be variant as it sees the Functions below[/color]
    [color=blue]Dim[/color] x [color=blue]As[/color] [color=blue]Long[/color], y [color=blue]As[/color] Long
    [color=blue]Dim[/color] TempString [color=blue]As[/color] String [color=lightgreen]'Tempory Strings used along the way[/color]
 
    [color=blue]ReDim[/color] vOut(1 [color=blue]To[/color] LongStrings.Rows.Count, 1 [color=blue]To[/color] LongStrings.Columns.Count)
        [color=blue]For[/color] x = 1 [color=blue]To[/color] LongStrings.Rows.Count
            [color=blue]For[/color] y = 1 [color=blue]To[/color] LongStrings.Columns.Count
                    [color=blue]Let[/color] TempString = Replace(Replace(LongStrings(x, y).Value, StartString, "|", 1), StopString, "|", InStr(1, Replace(LongStrings(x, y).Value, StartString, "|", 1), "|") + 1)
 
                    vOut(x, y) = Mid(TempString, 1, InStr(1, TempString, "|") - 1)
                    
                    Debug.Print vOut(x, y)
            [color=blue]Next[/color] y
        [color=blue]Next[/color] x
        UDArmyGeezerPickOutBit = vOut
 
[color=blue]End[/color] [color=blue]Function[/color] [color=lightgreen]'UDArmyGeezerPickOutBit[/color]


. It is not as complicated as it looks. Just lots of simple steps with simple functions like in the example I gave in Post #55 and #56 all stuck together in long line.
. I can open it up and explain everything in it in full detail if you or anyone asks.. Also it was the first attempt I tried. If you play around with different simple function combinations you can probably come up with infinite variations some of which would be shorter… (I’ll will probably post again anyway with another UDF and / or explanations to it for my own benefit for when the Thread gets revived again.. )
. If you have done it right then if you go back into your spreadsheet and type in any cell something like
=u
Then the usual Spreadsheet intellisense should give you an extra option
=UDArmyGeezerPickOutBit(
In the drop down suggestion list that usually pops up.

. Then you can test it easily by writing in some cell a formula which references a cell with one of those long strings , Like this for example (I shortened the long string a bit just for clarity of presentation here):

<b>Unknown</b><table width="10" cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>D</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">16</td><td style="text-align: center;background-color: #FF0000;;">Site1,OU=Groups,OU=DHSHQ-ONE-NET,OU=cloud,DC=cloud,DC=mypdc,DC=mynet</td></tr><tr ><td style="color: #161120;text-align: center;">17</td><td style="text-align: center;;"></td></tr><tr ><td style="color: #161120;text-align: center;">18</td><td style="text-align: center;;">DHSHQ-ONE-NET</td></tr></tbody></table><p style="width:7,2em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Master Sheet</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">D18</th><td style="text-align:left">=UDArmyGeezerPickOutBit(<font color="Blue">D16,"OU=Groups,OU=",","</font>)</td></tr></tbody></table></td></tr></table><br />


. At his point of course you can decide whether it is worth modifying the code to give you a column with those Picked out bits, or whether just to type that formula once in a new column alongside column D and dragged it down.
. NOTE: Once you have convinced yourself that Private Function is there and working, you may want to delete any formulas using it in the spreadsheet before running any codes. Otherwise the code seems to trigger the Function off erratically sometimes. (Not sure why yet. I may start a Thread asking for help on that one…)


. For now I have put the UDF in the full code, that is to say I access (Call) that function when I need it in the code.
. So here is a Code version I seem to have working now with your test data. I have not tested it thoroughly, and I think it is a bit much to take in one go. But as you surprised me that you got that last ragged code of mine working first time I thought I would post it to be going on with. But I expect we may want to discuss it further and it could be easier to swop some Workbooks / worksheet with data and / or the working macros in it.. This code uses a slightly different method along the lines of wot I did in post #23. It makes a temporary sheet rather than a temporary array for your unique Sheet name bits.

Code


Code:
[color=blue]Option[/color] [color=blue]Explicit[/color] [color=lightgreen]'Not necerssary but good practice to keep computer memery usage to minimum (and helps show up errors)[/color]
[color=blue]Sub[/color] ArmyGeezerUniqueSheet()
 
[color=lightgreen]'Application.ScreenUpdating = False 'Not necerssary but speeds things up a bit, by turning screen updating off. Usually best to 'Comment it out initially to see a bit better wot is going on.[/color]
[color=blue]Dim[/color] wksMasta [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksMasta = ThisWorkbook.Worksheets("Master Sheet") [color=lightgreen]'Give abbreviations the  Methods, properties, sub-Objects through dot of Worksheets Object[/color]
[color=blue]On[/color] [color=blue]Error[/color] [color=blue]GoTo[/color] TheEnd [color=lightgreen]'If anything goes wrong go to the End instead of crashing.[/color]
 
    [color=lightgreen]'1) Start Bit option to  Delete Sheets / Tabs------------[/color]
    [color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet [color=lightgreen]'ws now has Methods and Properties of Worksheets obtained with typing . dot after it![/color]
    Application.DisplayAlerts = [color=blue]False[/color] [color=lightgreen]'Prevents being asked everytime if you really want to delete the Workbook[/color]
    [color=blue]For[/color] [color=blue]Each[/color] ws [color=blue]In[/color] ActiveWorkbook.Worksheets
        [color=blue]If[/color] ws.Name <> "Master Sheet" And ws.Name <> "Masta sheet" [color=blue]Then[/color]
        ws.Delete
        [color=blue]Else[/color] [color=lightgreen]'Presumably then the worksheet name is FullDataSheet so[/color]
        [color=lightgreen]' do nothing (Don't delete it!)[/color]
        [color=blue]End[/color] [color=blue]If[/color]
    [color=blue]Next[/color]
    Application.DisplayAlerts = [color=blue]True[/color] [color=lightgreen]'Turn it back on[/color]
    [color=lightgreen]'End Bit to delete new Sheets / Tabs------------[/color]
 
    [color=lightgreen]'2) Bit to make new (Temporary) column for Picked out bits from long string in Column D[/color]
    wksMasta.Columns("E:E").Insert Shift:=xlToRight [color=lightgreen]'This inserts a new Column after Column D[/color]
    [color=blue]Dim[/color] lrMasta [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'last Roww in Masta Sheet.  Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647). Smaller numbers such as Byte, Integer etc. are typically converted to long in the computer so there are no Memory advantages of Dimensioning smaller..   http://www.mrexcel.com/forum/excel-questions/803662-byte-backward-loop-4.html[/color]
    [color=blue]Let[/color] lrMasta = wksMasta.Cells.Find(What:="*", after:=wksMasta.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (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]
    [color=blue]Let[/color] lrMasta = wksMasta.Cells(Rows.Count, 4).End(xlUp).Row [color=lightgreen]'Better alternative for single column work. - Starting at last Spreadsheet Row in column 4 (D), quasi "go back up" / return the last cell with entry in it as a Range object using .End Property and obtain rows from the .Rows Property of that returned Range Object[/color]
    [color=blue]Dim[/color] rngD [color=blue]As[/color] Range: [color=blue]Set[/color] rngD = wksMasta.Range("D3:D" & lrMasta & "") [color=lightgreen]'Data Column range .. Give abbreviations >>>[/color]
    [color=blue]Let[/color] rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") [color=lightgreen]'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")[/color]
 
[color=lightgreen]'3)Add new Worksheets----------------------------------[/color]
[color=lightgreen]'3a)First make 1 tempory sheet For Unique Sheet Names.[/color]
 
[color=blue]Let[/color] Worksheets.Add(after:=Worksheets(1)).Name = "Unique1" [color=lightgreen]'Add a Worksheet (after the First ever added or there), named Unique1 for now[/color]
[color=blue]Let[/color] wksMasta.Range("E1").Value = "Temp Heading" [color=lightgreen]'An annoying characteristic of advanced Filter is that it Requires a Heading (which it always copies with the other filtered cells)[/color]
wksMasta.Range("E1:E" & lrMasta & "").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Worksheets("Unique1").Range("A1"), Unique:=[color=blue]True[/color] [color=lightgreen]'Copies entire E Column to first column in sheet2 (Tempory made "Unique1" sheet), The important bit is Unique:=True - that only copies unique bits[/color]
[color=lightgreen]'---------------------[/color]
 
[color=blue]Dim[/color] LastUnqRow [color=blue]As[/color] [color=blue]Long[/color], UqeRow [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Rows in Tempory Unique sheet.[/color]
[color=blue]Let[/color] LastUnqRow = Worksheets("Unique1").Cells.Find(What:="*", after:=Worksheets("Unique1").Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'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)[/color]
  [color=lightgreen]'3b)go through making new sheets and copying filtered data to them[/color]
  [color=blue]Dim[/color] TempShtName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'Temporary Sheet Name for newly made sheet, not kept constant, used / updated in looping[/color]
  [color=blue]For[/color] UqeRow = 2 [color=blue]To[/color] LastUnqRow [color=blue]Step[/color] 1 [color=lightgreen]'[/color]
    'Make new sheets-----------------------
    [color=blue]If[/color] Sheets("Unique1").Cells(UqeRow, 1).Text <> "" [color=blue]Then[/color] [color=lightgreen]'Assuming a Record is there[/color]
    [color=blue]Let[/color] TempShtName = Sheets("Unique1").Cells(UqeRow, 1).Text [color=lightgreen]'Put name in Record variable[/color]
    [color=blue]Let[/color] Worksheets.Add(after:=Worksheets(1)).Name = TempShtName [color=lightgreen]'Add new worksheet with name TempShtName[/color]
  
  
      [color=blue]With[/color] wksMasta [color=lightgreen]'Copying data to new sheet----[/color]
        .UsedRange.AutoFilter Field:=5, Criteria1:=TempShtName [color=lightgreen]'Filter out everything except with that with the appropriate Record (makes visible based on the criteria (Column 5 (E)) only the stuff you want??)....[/color]
        .UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(TempShtName).Range("A1") [color=lightgreen]', 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]
      [color=blue]End[/color] [color=blue]With[/color] [color=lightgreen]'----------------------------------[/color]
    Application.DisplayAlerts = [color=blue]True[/color] [color=lightgreen]'This is normally just done once at the end, but is useful for debugging purposes to do here. It makes all of Master sheet re visible[/color]
      [color=blue]With[/color] Sheets(TempShtName).UsedRange [color=lightgreen]'Bit of simple Format Tidying up[/color]
        .WrapText = [color=blue]False[/color]
        .Columns.AutoFit
      [color=blue]End[/color] [color=blue]With[/color]
    [color=blue]Else[/color]
    [color=lightgreen]'Do nothing if no Record given[/color]
    [color=blue]End[/color] [color=blue]If[/color]
    [color=lightgreen]'-----------------------------[/color]
  [color=blue]Next[/color] UqeRow [color=lightgreen]'Go back and make another new sheet[/color]
[color=lightgreen]'------------------------------------------------------------[/color]
 wksMasta.AutoFilterMode = [color=blue]False[/color]
 
Application.DisplayAlerts = [color=blue]False[/color] [color=lightgreen]'Prevent being asked if you really want to delete Temporary Unique sheet[/color]
Worksheets("Unique1").Delete [color=lightgreen]' delete the Temporary the filtered name sheet as you do not need it any more[/color]
wksMasta.Columns("E:E").Delete [color=lightgreen]'Delete Tempory Column E (If you wont too!! - might want to comment this out if you want to keep it?)[/color]
Application.DisplayAlerts = [color=blue]True[/color]
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Turn screen "back on" or screen is "dead"[/color]
[color=blue]Exit[/color] [color=blue]Sub[/color] [color=lightgreen]'We stop code here assuming it worked (or at least did not crash!)[/color]
TheEnd:
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Important to do this here so if anything goes wrong then the screen updating is turned back on, ohterwisee the screen is dead[/color]
MsgBox (Err.Description) [color=lightgreen]'Print out error message in Message Box[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'ArmyGeezerUniqueSheet[/color]


Have Fun with it, maybe catch you later

Alan






P.s.1 I have not allowed for the possibility of the String bit you want not being in that long string in column D. There would be various ways of dealing with that, such as making the function return nothing or get the VBA code to check for that occurrence and act accordingly etc. etc. .. Just a bit more work. I noticed it was not in the D2 of your sample data, so I just start at Row 3 and miss it out

P.s. 2 Thanks RoryA. for the “RoaryLeftPubic” and other stuff from about Post #82 here..
http://www.mrexcel.com/forum/excel-...ic-applications-evaluate-range-vlookup-9.html
without stuff learnt from there I would not be Functioning here (Publically :laugh: )
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi again,
. This aint too important, … I was just tidying up and was about to trash this .xlsx file
https://app.box.com/s/ts4aiwn6n997ow2tvv3xuettpj76oj7s
. Thought I would drop it off. Take a quick look then trash it.
. It is wot I knocked up to get started on your stuff. You’ll see there are a few colors highlighting and I have reduced those long strings a bit.
. I think there is just about enough representative data there. Just worth bearing in mind for the next time. The more obvious you make it wot you want, the better chance you get of someone helping you.
. Even then I agree the Mr Excel HTML screen shot is still a bit big and difficult to see. (Reducing the screen zoom can help there a bit, and typically you see more when logged in and the advertisements go away!) But you should still try to learn to use it – it is preferred here so everyone can “join in”

. As always “A Picture paints a thousand words” (but sometimes a Workbook can paint a lot of pictures!)

. The screen shot is a bit more readable now…

<b></b><table width="10" cellpadding="1px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>C</th><th>D</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">description</td><td style="text-align: center;;">memberOf</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Built-in account for administering the computer/domain</td><td style="text-align: center;;">cloud,DC=mypdc,DC=mynet</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="background-color: #FFFF00;;">GSA SQL DBA Services Account with admin rights for the installation and maintanence GSA Cloud SQL Databases Servers.</td><td style="text-align: center;;">ess,OU=Groups,OU=APP-GRP,DC=clou</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="background-color: #7F7F7F;;">Accounts in the GSA Cloud Environment for UNIX Team</td><td style="text-align: center;;">Site1,OU=Groups,OU=GSA-OCSIT-WEBSITES,OU=cloud,DC=cloud,</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="background-color: #FFFF00;;">Accounts in the GSA Cloud Environment for UNIX Team</td><td style="text-align: center;;">Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=mypdc,DC=mynet;CN=P</td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="background-color: #FFFF00;;">Accounts in the GSA Cloud Environment for UNIX Team</td><td style="text-align: center;;">nal_Services_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=myp</td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="background-color: #92D050;;">Level 1</td><td style="text-align: center;;">upport_Site1,OU=Groups,OU=COHBE,OU=cloud,DC=cloud,DC=ps,OU=G</td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="background-color: #00B0F0;;">Level 1 Supervisor on Duty</td><td style="text-align: center;;">ort_Site1,OU=Groups,OU=DHSNG,OU=cloud,DC=clou</td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="background-color: #FFFF00;;">Patrol Agent Service Account used for monitoring system.</td><td style="text-align: center;;">rvices_Access,OU=Groups,OU=APP-GRP,DC=clou</td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="background-color: #FFFF00;;">my Users used for initial linux vm deployment to provision user accounts, and for template maintenance</td><td style="text-align: center;;">vices_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=</td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="background-color: #FFFF00;;">my Users used for initial windows vm deployment to provision user accounts, and for template maintenance</td><td style="text-align: center;;">net;CN=Terminal_Services_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=mypdc,DC=mynet</td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="background-color: #7030A0;;">Senior Consultant</td><td style="text-align: center;;">Pubish       ,OU=Groups,OU=my-ADMIN-PDC-GSA,OU=cloud,DC=cloud,DC=mypdc,DC=mynet;CN=my_Org_vcd_Author,</td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: right;background-color: #D99795;;"></td><td style="text-align: center;;">Users@myF-DEMO-MONT,OU=Groups,OU=myF-DEMO-MONT,OU=clo</td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style="text-align: right;background-color: #E46D0A;;"></td><td style="text-align: center;;">l_Users,OU=Groups,OU=myF-ADIP-BIO,OU=cloud,DC=cloud,DC=mypdc,</td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style="background-color: #93CDDD;;">GIS DBA Support</td><td style="text-align: center;;">njdca_VMware_Support_Site2,OU=Groups,OU=NJDCA,OU=cloud,DC=cloud</td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style="background-color: #FF0000;;">my Level 1</td><td style="text-align: center;;">Site1,OU=Groups,OU=DHSHQ-ONE-NET,OU=cloud,DC=cloud,DC=mypd</td></tr></tbody></table><p style="width:3,6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Master Sheet</p><br /><br />


……….




[Table="class: grid"][tr][td] [/td][td]
C
[/td][td]
D
[/td][/tr]
[tr][td]
1
[/td][td]
description​
[/td][td]
memberOf​
[/td][/tr]

[tr][td]
2
[/td][td]
Built-in account for administering the computer/domain​
[/td][td]
cloud,DC=mypdc,DC=mynet​
[/td][/tr]

[tr][td]
3
[/td][td]
GSA SQL DBA Services Account with admin rights for the installation and maintanence GSA Cloud SQL Databases Servers.​
[/td][td]
ess,OU=Groups,OU=APP-GRP,DC=clou​
[/td][/tr]

[tr][td]
4
[/td][td]
Accounts in the GSA Cloud Environment for UNIX Team​
[/td][td]
Site1,OU=Groups,OU=GSA-OCSIT-WEBSITES,OU=cloud,DC=cloud,​
[/td][/tr]

[tr][td]
5
[/td][td]
Accounts in the GSA Cloud Environment for UNIX Team​
[/td][td]
Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=mypdc,DC=mynet;CN=P​
[/td][/tr]

[tr][td]
6
[/td][td]
Accounts in the GSA Cloud Environment for UNIX Team​
[/td][td]
nal_Services_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=myp​
[/td][/tr]

[tr][td]
7
[/td][td]
Level 1​
[/td][td]
upport_Site1,OU=Groups,OU=COHBE,OU=cloud,DC=cloud,DC=ps,OU=G​
[/td][/tr]

[tr][td]
8
[/td][td]
Level 1 Supervisor on Duty​
[/td][td]
ort_Site1,OU=Groups,OU=DHSNG,OU=cloud,DC=clou​
[/td][/tr]

[tr][td]
9
[/td][td]
Patrol Agent Service Account used for monitoring system.​
[/td][td]
rvices_Access,OU=Groups,OU=APP-GRP,DC=clou​
[/td][/tr]

[tr][td]
10
[/td][td]
my Users used for initial linux vm deployment to provision user accounts, and for template maintenance​
[/td][td]
vices_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=​
[/td][/tr]

[tr][td]
11
[/td][td]
my Users used for initial windows vm deployment to provision user accounts, and for template maintenance​
[/td][td]
net;CN=Terminal_Services_Access,OU=Groups,OU=APP-GRP,DC=cloud,DC=mypdc,DC=mynet​
[/td][/tr]

[tr][td]
12
[/td][td]
Senior Consultant​
[/td][td]
Pubish ,OU=Groups,OU=my-ADMIN-PDC-GSA,OU=cloud,DC=cloud,DC=mypdc,DC=mynet;CN=my_Org_vcd_Author,​
[/td][/tr]

[tr][td]
13
[/td][td] [/td][td]
Users@myF-DEMO-MONT,OU=Groups,OU=myF-DEMO-MONT,OU=clo​
[/td][/tr]

[tr][td]
14
[/td][td] [/td][td]
l_Users,OU=Groups,OU=myF-ADIP-BIO,OU=cloud,DC=cloud,DC=mypdc,​
[/td][/tr]

[tr][td]
15
[/td][td]
GIS DBA Support​
[/td][td]
njdca_VMware_Support_Site2,OU=Groups,OU=NJDCA,OU=cloud,DC=cloud​
[/td][/tr]

[tr][td]
16
[/td][td]
my Level 1​
[/td][td]
Site1,OU=Groups,OU=DHSHQ-ONE-NET,OU=cloud,DC=cloud,DC=mypd​
[/td][/tr]

[tr][td]
17
[/td][td] [/td][td] [/td][/tr]

[tr][td]
18
[/td][td] [/td][td] [/td][/tr]
[/table]



But also a full file as well like wot you sent is also good
 
Upvote 0
Hi - I've read through this thread and I didn't see a macro that maintained formulas when copying data from one worksheet to another, did I miss this? Is there a macro that does maintain formulas?

Any help would be great.

Thanks!
 
Upvote 0
Hi - I've read through this thread and I didn't see a macro that maintained formulas when copying data from one worksheet to another, did I miss this? ........

Hi..

. Are Yous for real or taking the P__s (no offence - only light hearted comment – the Thread is a bit long I suppose)

.. But it is hard to believe you read through the thread:- wot about Post #32 to #40 !!!!!.

. And the Thread that I started to clarify that point about maintained formulas when copying data from one worksheet..(which I also referenced in this Thread already):-..
http://www.mrexcel.com/forum/excel-...ypevisible-copy-only-values-not-formulas.html

. Anyways while I am ‘ere

My last Code with a modification to ensure Formulas are copied. Modification shown in Purple




Code:
[color=blue]Option[/color] [color=blue]Explicit[/color] [color=lightgreen]'Not necerssary but good practice to keep computer memery usage to minimum (and helps show up errors)[/color]
[color=blue]Sub[/color] ArmyGeezerUniqueSheet()
 
Application.ScreenUpdating = [color=blue]False[/color] [color=lightgreen]'Not necerssary but speeds things up a bit, by turning screen updating off. Usually best to 'Comment it out initially to see a bit better wot is going on.[/color]
[color=blue]Dim[/color] wksMasta [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksMasta = ThisWorkbook.Worksheets("Master Sheet") [color=lightgreen]'Give abbreviations the  Methods, properties, sub-Objects through dot of Worksheets Object[/color]
[color=blue]On[/color] [color=blue]Error[/color] [color=blue]GoTo[/color] TheEnd [color=lightgreen]'If anything goes wrong go to the End instead of crashing.[/color]
 
    [color=lightgreen]'1) Start Bit option to  Delete Sheets / Tabs------------[/color]
    [color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet [color=lightgreen]'ws now has Methods and Properties of Worksheets obtained with typing . dot after it![/color]
    Application.DisplayAlerts = [color=blue]False[/color] [color=lightgreen]'Prevents being asked everytime if you really want to delete the Workbook[/color]
    [color=blue]For[/color] [color=blue]Each[/color] ws [color=blue]In[/color] ActiveWorkbook.Worksheets
        [color=blue]If[/color] ws.Name <> "Master Sheet" And ws.Name <> "Masta sheet" And ws.Name <> "AnySheetYouWantToKeep" [color=blue]Then[/color]
        ws.Delete
        [color=blue]Else[/color] [color=lightgreen]'Presumably then the worksheet name is any one you want to keep so...[/color]
        [color=lightgreen]'....do nothing (Don't delete it!)[/color]
        [color=blue]End[/color] [color=blue]If[/color]
    [color=blue]Next[/color]
    Application.DisplayAlerts = [color=blue]True[/color] [color=lightgreen]'Turn it back on[/color]
    [color=lightgreen]'End Bit to delete new Sheets / Tabs------------[/color]
 
    [color=lightgreen]'2) Bit to make new (Temporary) column for Picked out bits from long string in Column D[/color]
    wksMasta.Columns("E:E").Insert Shift:=xlToRight [color=lightgreen]'This inserts a new Column after Column D[/color]
    [color=blue]Dim[/color] lrMasta [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'last Roww in Masta Sheet.  Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647). Smaller numbers such as Byte, Integer etc. are typically converted to long in the computer so there are no Memory advantages of Dimensioning smaller..   http://www.mrexcel.com/forum/excel-questions/803662-byte-backward-loop-4.html[/color]
    [color=blue]Let[/color] lrMasta = wksMasta.Cells.Find(What:="*", after:=wksMasta.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (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]
    [color=blue]Let[/color] lrMasta = wksMasta.Cells(Rows.Count, 4).End(xlUp).Row [color=lightgreen]'Better alternative for single column work. - Starting at last Spreadsheet Row in column 4 (D), quasi "go back up" / return the last cell with entry in it as a Range object using .End Property and obtain rows from the .Rows Property of that returned Range Object[/color]
    [color=blue]Dim[/color] rngD [color=blue]As[/color] Range: [color=blue]Set[/color] rngD = wksMasta.Range("D3:D" & lrMasta & "") [color=lightgreen]'Data Column range .. Give abbreviations >>>[/color]
    [color=blue]Let[/color] rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") [color=lightgreen]'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")[/color]
    [color=lightgreen]'End bit to make Temporary column[/color]
[color=lightgreen]'3)Add new Worksheets----------------------------------[/color]
[color=lightgreen]'3a)First make 1 tempory sheet For Unique Sheet Names.[/color]
 
[color=blue]Let[/color] Worksheets.Add(after:=Worksheets(1)).Name = "Unique1" [color=lightgreen]'Add a Worksheet (after the First ever added or there), named Unique1 for now[/color]
[color=blue]Let[/color] wksMasta.Range("E1").Value = "Temp Heading" [color=lightgreen]'An annoying characteristic of advanced Filter is that it Requires a Heading (which it always copies with the other filtered cells)[/color]
wksMasta.Range("E1:E" & lrMasta & "").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Worksheets("Unique1").Range("A1"), Unique:=[color=blue]True[/color] [color=lightgreen]'Copies entire E Column to first column in sheet2 (Tempory made "Unique1" sheet), The important bit is Unique:=True - that only copies unique bits[/color]
[color=lightgreen]'---------------------[/color]
 
[color=blue]Dim[/color] LastUnqRow [color=blue]As[/color] [color=blue]Long[/color], UqeRow [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Rows in Tempory Unique sheet. As before unless if you need sort of validation (i.e. the value should only be within the range of a Byte/Integer) there's no point using anything but Long.-- 'saving' memory and using the smallest bit. But upon/after 32-bit, by all I have read, Integers (Short) need converted internally anyways, so a Long is actually faster.   http://www.mrexcel.com/forum/excel-questions/803662-byte-backward-loop-4.html[/color]
[color=blue]Let[/color] LastUnqRow = Worksheets("Unique1").Cells.Find(What:="*", after:=Worksheets("Unique1").Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'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)[/color]
  [color=lightgreen]'3b)go through making new sheets and copying filtered data to them[/color]
  [color=blue]Dim[/color] TempShtName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'Temporary Sheet Name for newly made sheet, not kept constant, used / updated in looping[/color]
  [color=blue]For[/color] UqeRow = 2 [color=blue]To[/color] LastUnqRow [color=blue]Step[/color] 1 [color=lightgreen]'[/color]
    'Make new sheets-----------------------
    [color=blue]If[/color] Sheets("Unique1").Cells(UqeRow, 1).Text <> "" [color=blue]Then[/color] [color=lightgreen]'Assuming a Record is there[/color]
    [color=blue]Let[/color] TempShtName = Sheets("Unique1").Cells(UqeRow, 1).Text [color=lightgreen]'Put name in Record variable[/color]
    [color=blue]Let[/color] Worksheets.Add(after:=Worksheets(1)).Name = TempShtName [color=lightgreen]'Add new worksheet with name TempShtName[/color]
  
  
      [color=blue]With[/color] wksMasta [color=lightgreen]'Copying data to new sheet----[/color]
        .UsedRange.AutoFilter Field:=5, Criteria1:=TempShtName [color=lightgreen]'Filter out everything except with that with the appropriate Record (makes visible based on the criteria (Column 5 (E)) only the stuff you want??)....[/color]
        .UsedRange.SpecialCells(xlCellTypeVisible).Copy [color=lightgreen]'Destination:=Worksheets(TempShtName).Range("A1") ', 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]
        [color=purple]Worksheets(TempShtName).Range("A1").PasteSpecial Paste:=xlPasteFormulas[/color][color=lightgreen]'This is necerssary to "ensure" formulas are copied. Comment out Destination:= above if you out use this    http://www.mrexcel.com/forum/excel-questions/828241-visual-basic-applications-autofilter-specialcells-xlcelltypevisible-copy-only-values-not-formulas.html[/color]
        [color=lightgreen]'Worksheets(TempShtName).Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme 'This is necerssary to "ensure" "everything" is copied. Comment out Destination:= above if you use  this    For example   http://www.excelforum.com/excel-new-users-basics/1063110-sheet-range-object-item-list-reorder.html[/color]
      [color=blue]End[/color] [color=blue]With[/color] [color=lightgreen]'----------------------------------[/color]
    Application.DisplayAlerts = [color=blue]True[/color] [color=lightgreen]'This is normally just done once at the end, but is useful for debugging purposes to do here. It makes all of Master sheet re visible[/color]
      [color=blue]With[/color] Sheets(TempShtName).UsedRange [color=lightgreen]'Bit of simple Format Tidying up[/color]
        .WrapText = [color=blue]False[/color]
        .Columns.AutoFit
      [color=blue]End[/color] [color=blue]With[/color]
    [color=blue]Else[/color]
    [color=lightgreen]'Do nothing if no Record given[/color]
    [color=blue]End[/color] [color=blue]If[/color]
    [color=lightgreen]'-----------------------------[/color]
  [color=blue]Next[/color] UqeRow [color=lightgreen]'Go back and make another new sheet[/color]
[color=lightgreen]'------------------------------------------------------------[/color]
wksMasta.AutoFilterMode = [color=blue]False[/color] [color=lightgreen]'Make everything visible in the First Master Sheet[/color]
 
Application.DisplayAlerts = [color=blue]False[/color] [color=lightgreen]'Prevent being asked if you really want to delete Temporary Unique sheet[/color]
Worksheets("Unique1").Delete [color=lightgreen]' delete the Temporary the filtered name sheet as you do not need it any more[/color]
wksMasta.Columns("E:E").Delete [color=lightgreen]'Delete Tempory Column E (If you wont too!! - might want to comment this out if you want to keep it?)[/color]
Application.DisplayAlerts = [color=blue]True[/color]
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Turn screen "back on" or screen is "dead"[/color]
[color=blue]Exit[/color] [color=blue]Sub[/color] [color=lightgreen]'We stop code here assuming it worked (or at least did not crash!)[/color]
TheEnd:
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Important to do this here so if anything goes wrong then the screen updating is turned back on, ohterwisee the screen is dead[/color]
MsgBox (Err.Description) [color=lightgreen]'Print out error message in Message Box[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'ArmyGeezerUniqueSheet[/color]
 
Last edited:
Upvote 0
Haha I did read through it and it wasn't clear which code was which. Other readers were asking for different things than I was at times. And I'm not sure which language you speak but some of your writing was hard to understand.

Bottom line: This is very helpful, sorry if you thought I was full of BS
 
Upvote 0
Tried this and I got an error message, "Compile Error Sub or Function not defined"

And it highlighted the bold portion of the code:

Let rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") 'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")
 
Upvote 0
Hi Army man,
. ……..done a code for ya..

. ... I took the unusual (for me) starting point of actually writing a “UDF”, (User Defined Function), to do that initial bit of getting the shortened string you want to use as Filtering criteria and New Sheet Names.
.
. (I always prefer to keep everything in one code especially when I try to give it to anyone.. But I have to confess it starts getting a bit too big and messy. …. And I woz practicing these things just now… …….)

.
.
. FYI in case you don’t know. When you make a UDF you have a new “Home Made” Function. Let’s call it “UDArmyGeezerPickOutBit”.
.
. The nice thing is if you stick it in a Normal Module as a

Public Function UDArmyGeezerPickOutBit(LongStrings As Rang____________etc.
.
….. then you have it available both as a normal Spreadsheet Function and can use it in a VBA Code. Nice ‘cos you can check it works first in the Spread sheet.
.
. So get the UDF up and running before you do anything else..
.
. Copy and paste this in a normal Module.


Code:
[COLOR=blue]Public[/COLOR] [COLOR=blue]Function[/COLOR] UDArmyGeezerPickOutBit(LongStrings [COLOR=blue]As[/COLOR] Range, StartString [COLOR=blue]As[/COLOR] String, StopString [COLOR=blue]As[/COLOR] String)
 
    [COLOR=blue]Dim[/COLOR] vOut() [COLOR=blue]As[/COLOR] [COLOR=blue]Variant[/COLOR] [COLOR=lightgreen]' Array but individual elements Has to be variant as it sees the Functions below[/COLOR]
    [COLOR=blue]Dim[/COLOR] x [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR], y [COLOR=blue]As[/COLOR] Long
    [COLOR=blue]Dim[/COLOR] TempString [COLOR=blue]As[/COLOR] String [COLOR=lightgreen]'Tempory Strings used along the way[/COLOR]
 
    [COLOR=blue]ReDim[/COLOR] vOut(1 [COLOR=blue]To[/COLOR] LongStrings.Rows.Count, 1 [COLOR=blue]To[/COLOR] LongStrings.Columns.Count)
        [COLOR=blue]For[/COLOR] x = 1 [COLOR=blue]To[/COLOR] LongStrings.Rows.Count
            [COLOR=blue]For[/COLOR] y = 1 [COLOR=blue]To[/COLOR] LongStrings.Columns.Count
                    [COLOR=blue]Let[/COLOR] TempString = Replace(Replace(LongStrings(x, y).Value, StartString, "|", 1), StopString, "|", InStr(1, Replace(LongStrings(x, y).Value, StartString, "|", 1), "|") + 1)
 
                    vOut(x, y) = Mid(TempString, 1, InStr(1, TempString, "|") - 1)
                    
                    Debug.Print vOut(x, y)
            [COLOR=blue]Next[/COLOR] y
        [COLOR=blue]Next[/COLOR] x
        UDArmyGeezerPickOutBit = vOut
 
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR] [COLOR=lightgreen]'UDArmyGeezerPickOutBit[/COLOR]


. It is not as complicated as it looks. Just lots of simple steps with simple functions like in the example I gave in Post #55 and #56 all stuck together in long line.
. I can open it up and explain everything in it in full detail if you or anyone asks.. Also it was the first attempt I tried. If you play around with different simple function combinations you can probably come up with infinite variations some of which would be shorter… (I’ll will probably post again anyway with another UDF and / or explanations to it for my own benefit for when the Thread gets revived again.. )
. If you have done it right then if you go back into your spreadsheet and type in any cell something like
=u
Then the usual Spreadsheet intellisense should give you an extra option
=UDArmyGeezerPickOutBit(
In the drop down suggestion list that usually pops up.

. Then you can test it easily by writing in some cell a formula which references a cell with one of those long strings , Like this for example (I shortened the long string a bit just for clarity of presentation here):

Unknown[TABLE="width: 10"]
<colgroup><col style="background-color: #E0E0F0" width="25px"><col></colgroup><thead>[TR="bgcolor: #E0E0F0"]
[TH][/TH]
[TH]D[/TH]
[/TR]
</thead><tbody>[TR]
[TD="align: center"]16[/TD]
[TD="bgcolor: #FF0000, align: center"]Site1,OU=Groups,OU=DHSHQ-ONE-NET,OU=cloud,DC=cloud,DC=mypdc,DC=mynet[/TD]
[/TR]
[TR]
[TD="align: center"]17[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]18[/TD]
[TD="align: center"]DHSHQ-ONE-NET[/TD]
[/TR]
</tbody>[/TABLE]
Master Sheet

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: #E0E0F0"]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: #E0E0F0"]D18[/TH]
[TD="align: left"]=UDArmyGeezerPickOutBit(D16,"OU=Groups,OU=",",")[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]




. At his point of course you can decide whether it is worth modifying the code to give you a column with those Picked out bits, or whether just to type that formula once in a new column alongside column D and dragged it down.
. NOTE: Once you have convinced yourself that Private Function is there and working, you may want to delete any formulas using it in the spreadsheet before running any codes. Otherwise the code seems to trigger the Function off erratically sometimes. (Not sure why yet. I may start a Thread asking for help on that one…)


. For now I have put the UDF in the full code, that is to say I access (Call) that function when I need it in the code.
. So here is a Code version I seem to have working now with your test data. I have not tested it thoroughly, and I think it is a bit much to take in one go. But as you surprised me that you got that last ragged code of mine working first time I thought I would post it to be going on with. But I expect we may want to discuss it further and it could be easier to swop some Workbooks / worksheet with data and / or the working macros in it.. This code uses a slightly different method along the lines of wot I did in post #23. It makes a temporary sheet rather than a temporary array for your unique Sheet name bits.

Code


Code:
[COLOR=blue]Option[/COLOR] [COLOR=blue]Explicit[/COLOR] [COLOR=lightgreen]'Not necerssary but good practice to keep computer memery usage to minimum (and helps show up errors)[/COLOR]
[COLOR=blue]Sub[/COLOR] ArmyGeezerUniqueSheet()
 
[COLOR=lightgreen]'Application.ScreenUpdating = False 'Not necerssary but speeds things up a bit, by turning screen updating off. Usually best to 'Comment it out initially to see a bit better wot is going on.[/COLOR]
[COLOR=blue]Dim[/COLOR] wksMasta [COLOR=blue]As[/COLOR] Worksheet: [COLOR=blue]Set[/COLOR] wksMasta = ThisWorkbook.Worksheets("Master Sheet") [COLOR=lightgreen]'Give abbreviations the  Methods, properties, sub-Objects through dot of Worksheets Object[/COLOR]
[COLOR=blue]On[/COLOR] [COLOR=blue]Error[/COLOR] [COLOR=blue]GoTo[/COLOR] TheEnd [COLOR=lightgreen]'If anything goes wrong go to the End instead of crashing.[/COLOR]
 
    [COLOR=lightgreen]'1) Start Bit option to  Delete Sheets / Tabs------------[/COLOR]
    [COLOR=blue]Dim[/COLOR] ws [COLOR=blue]As[/COLOR] Worksheet [COLOR=lightgreen]'ws now has Methods and Properties of Worksheets obtained with typing . dot after it![/COLOR]
    Application.DisplayAlerts = [COLOR=blue]False[/COLOR] [COLOR=lightgreen]'Prevents being asked everytime if you really want to delete the Workbook[/COLOR]
    [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] ws [COLOR=blue]In[/COLOR] ActiveWorkbook.Worksheets
        [COLOR=blue]If[/COLOR] ws.Name <> "Master Sheet" And ws.Name <> "Masta sheet" [COLOR=blue]Then[/COLOR]
        ws.Delete
        [COLOR=blue]Else[/COLOR] [COLOR=lightgreen]'Presumably then the worksheet name is FullDataSheet so[/COLOR]
        [COLOR=lightgreen]' do nothing (Don't delete it!)[/COLOR]
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Next[/COLOR]
    Application.DisplayAlerts = [COLOR=blue]True[/COLOR] [COLOR=lightgreen]'Turn it back on[/COLOR]
    [COLOR=lightgreen]'End Bit to delete new Sheets / Tabs------------[/COLOR]
 
    [COLOR=lightgreen]'2) Bit to make new (Temporary) column for Picked out bits from long string in Column D[/COLOR]
    wksMasta.Columns("E:E").Insert Shift:=xlToRight [COLOR=lightgreen]'This inserts a new Column after Column D[/COLOR]
    [COLOR=blue]Dim[/COLOR] lrMasta [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] [COLOR=lightgreen]'last Roww in Masta Sheet.  Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647). Smaller numbers such as Byte, Integer etc. are typically converted to long in the computer so there are no Memory advantages of Dimensioning smaller..   http://www.mrexcel.com/forum/excel-questions/803662-byte-backward-loop-4.html[/COLOR]
    [COLOR=blue]Let[/COLOR] lrMasta = wksMasta.Cells.Find(What:="*", after:=wksMasta.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [COLOR=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (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]
    [COLOR=blue]Let[/COLOR] lrMasta = wksMasta.Cells(Rows.Count, 4).End(xlUp).Row [COLOR=lightgreen]'Better alternative for single column work. - Starting at last Spreadsheet Row in column 4 (D), quasi "go back up" / return the last cell with entry in it as a Range object using .End Property and obtain rows from the .Rows Property of that returned Range Object[/COLOR]
    [COLOR=blue]Dim[/COLOR] rngD [COLOR=blue]As[/COLOR] Range: [COLOR=blue]Set[/COLOR] rngD = wksMasta.Range("D3:D" & lrMasta & "") [COLOR=lightgreen]'Data Column range .. Give abbreviations >>>[/COLOR]
    [COLOR=blue]Let[/COLOR] rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") [COLOR=lightgreen]'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")[/COLOR]
 
[COLOR=lightgreen]'3)Add new Worksheets----------------------------------[/COLOR]
[COLOR=lightgreen]'3a)First make 1 tempory sheet For Unique Sheet Names.[/COLOR]
 
[COLOR=blue]Let[/COLOR] Worksheets.Add(after:=Worksheets(1)).Name = "Unique1" [COLOR=lightgreen]'Add a Worksheet (after the First ever added or there), named Unique1 for now[/COLOR]
[COLOR=blue]Let[/COLOR] wksMasta.Range("E1").Value = "Temp Heading" [COLOR=lightgreen]'An annoying characteristic of advanced Filter is that it Requires a Heading (which it always copies with the other filtered cells)[/COLOR]
wksMasta.Range("E1:E" & lrMasta & "").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Worksheets("Unique1").Range("A1"), Unique:=[COLOR=blue]True[/COLOR] [COLOR=lightgreen]'Copies entire E Column to first column in sheet2 (Tempory made "Unique1" sheet), The important bit is Unique:=True - that only copies unique bits[/COLOR]
[COLOR=lightgreen]'---------------------[/COLOR]
 
[COLOR=blue]Dim[/COLOR] LastUnqRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR], UqeRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] [COLOR=lightgreen]'Rows in Tempory Unique sheet.[/COLOR]
[COLOR=blue]Let[/COLOR] LastUnqRow = Worksheets("Unique1").Cells.Find(What:="*", after:=Worksheets("Unique1").Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [COLOR=lightgreen]'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)[/COLOR]
  [COLOR=lightgreen]'3b)go through making new sheets and copying filtered data to them[/COLOR]
  [COLOR=blue]Dim[/COLOR] TempShtName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=lightgreen]'Temporary Sheet Name for newly made sheet, not kept constant, used / updated in looping[/COLOR]
  [COLOR=blue]For[/COLOR] UqeRow = 2 [COLOR=blue]To[/COLOR] LastUnqRow [COLOR=blue]Step[/COLOR] 1 [COLOR=lightgreen]'[/COLOR]
    'Make new sheets-----------------------
    [COLOR=blue]If[/COLOR] Sheets("Unique1").Cells(UqeRow, 1).Text <> "" [COLOR=blue]Then[/COLOR] [COLOR=lightgreen]'Assuming a Record is there[/COLOR]
    [COLOR=blue]Let[/COLOR] TempShtName = Sheets("Unique1").Cells(UqeRow, 1).Text [COLOR=lightgreen]'Put name in Record variable[/COLOR]
    [COLOR=blue]Let[/COLOR] Worksheets.Add(after:=Worksheets(1)).Name = TempShtName [COLOR=lightgreen]'Add new worksheet with name TempShtName[/COLOR]
  
  
      [COLOR=blue]With[/COLOR] wksMasta [COLOR=lightgreen]'Copying data to new sheet----[/COLOR]
        .UsedRange.AutoFilter Field:=5, Criteria1:=TempShtName [COLOR=lightgreen]'Filter out everything except with that with the appropriate Record (makes visible based on the criteria (Column 5 (E)) only the stuff you want??)....[/COLOR]
        .UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(TempShtName).Range("A1") [COLOR=lightgreen]', 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]
      [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR] [COLOR=lightgreen]'----------------------------------[/COLOR]
    Application.DisplayAlerts = [COLOR=blue]True[/COLOR] [COLOR=lightgreen]'This is normally just done once at the end, but is useful for debugging purposes to do here. It makes all of Master sheet re visible[/COLOR]
      [COLOR=blue]With[/COLOR] Sheets(TempShtName).UsedRange [COLOR=lightgreen]'Bit of simple Format Tidying up[/COLOR]
        .WrapText = [COLOR=blue]False[/COLOR]
        .Columns.AutoFit
      [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
    [COLOR=blue]Else[/COLOR]
    [COLOR=lightgreen]'Do nothing if no Record given[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=lightgreen]'-----------------------------[/COLOR]
  [COLOR=blue]Next[/COLOR] UqeRow [COLOR=lightgreen]'Go back and make another new sheet[/COLOR]
[COLOR=lightgreen]'------------------------------------------------------------[/COLOR]
 wksMasta.AutoFilterMode = [COLOR=blue]False[/COLOR]
 
Application.DisplayAlerts = [COLOR=blue]False[/COLOR] [COLOR=lightgreen]'Prevent being asked if you really want to delete Temporary Unique sheet[/COLOR]
Worksheets("Unique1").Delete [COLOR=lightgreen]' delete the Temporary the filtered name sheet as you do not need it any more[/COLOR]
wksMasta.Columns("E:E").Delete [COLOR=lightgreen]'Delete Tempory Column E (If you wont too!! - might want to comment this out if you want to keep it?)[/COLOR]
Application.DisplayAlerts = [COLOR=blue]True[/COLOR]
Application.ScreenUpdating = [COLOR=blue]True[/COLOR] [COLOR=lightgreen]'Turn screen "back on" or screen is "dead"[/COLOR]
[COLOR=blue]Exit[/COLOR] [COLOR=blue]Sub[/COLOR] [COLOR=lightgreen]'We stop code here assuming it worked (or at least did not crash!)[/COLOR]
TheEnd:
Application.ScreenUpdating = [COLOR=blue]True[/COLOR] [COLOR=lightgreen]'Important to do this here so if anything goes wrong then the screen updating is turned back on, ohterwisee the screen is dead[/COLOR]
MsgBox (Err.Description) [COLOR=lightgreen]'Print out error message in Message Box[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR] [COLOR=lightgreen]'ArmyGeezerUniqueSheet[/COLOR]


Have Fun with it, maybe catch you later

Alan






P.s.1 I have not allowed for the possibility of the String bit you want not being in that long string in column D. There would be various ways of dealing with that, such as making the function return nothing or get the VBA code to check for that occurrence and act accordingly etc. etc. .. Just a bit more work. I noticed it was not in the D2 of your sample data, so I just start at Row 3 and miss it out

P.s. 2 Thanks RoryA. for the “RoaryLeftPubic” and other stuff from about Post #82 here..
http://www.mrexcel.com/forum/excel-...ic-applications-evaluate-range-vlookup-9.html
without stuff learnt from there I would not be Functioning here (Publically :laugh: )


ArmyGeezer, nice touch!

So I am getting an error at

Let rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") 'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")


Saying that UDArmyGeezer is not defined.
 
Upvote 0
.......

So I am getting an error at

Let rngD.Offset(0, 1).Value = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",") 'Our UDF is set to work with our Range in Column D. The Offset Property bit is just a neat - here it returns a range that is one place to the right. That saves us having to Dimension and set a rngE to use in an alternative code line RngE = UDArmyGeezerPickOutBit(rngD, "OU=Groups,OU=", ",")


Saying that UDArmyGeezer is not defined.

Hi
. Try not to quote everything when you reply unless it is really relevant.

.
........
.
. So get the UDF up and running before you do anything else..
.
.........


. Did you get the point about The Public Function? That is to say you need to create a separate Public Function?
. read carefully everything again in my Post #61

Alan…
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top