Results 1 to 4 of 4

Thread: [RESOLVED] Creating unique list and writing to files

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    2

    Resolved [RESOLVED] Creating unique list and writing to files

    Hello there!

    I'm the definition of a newbie so please be a bit paitent.

    I have an Excel sheet with quite much information (about 13000 rows).
    The interesting columns are A, D and E containing the following information:
    - Column A contains a long list of names, all unique.
    - Column D contains "group names".
    - Column E contains Owners of the groups.
    (Look at it as product names, product groups and responible persons for the groups.)
    Row 1 contains the text "Names", "Group Names" and "Owners", the actual data starts at row 2.

    What I want to do is:
    1. Create a new sheet named "Owners" containing a list of all the group names and all owners associated with the groups.
    2. Create a bunch of files named after the group names and the extention ".ext" e.g. <group1.ext>. containing the first row [Names] and then the all the names (one on each row) for that group listed below.

    Example: ( [] = Cell)
    [Names][ ][ ][Group Names][Owners]
    [tcce1][ ][ ][group1 ][peter;john;sarah;]
    [tcce2][ ][ ][group1 ][peter;john;liza;]
    [rc_f ][ ][ ][group2 ][kent;sarah;]

    This would result in a list on the Owners sheet looking like this:

    [Group Name][Owners]
    [group1 ][peter;john;sarah;liza]
    [group2 ][kent;sarah;]

    The file "group1.ext" (ordinary text file) should contain exactly the following:
    [Names]
    tcce1
    tcce2

    I'm using a form with two buttons to execute the 2 different Subs. I've managed to create a sheet named Owners and to create a list. The list is incorrect in some way though.

    VB Code:
    1. Private Sub CmdButGenExtFiles_Click()
    2.  
    3. End Sub
    4.  
    5. Private Sub CmdButGenOwnList_Click()
    6.  
    7.     Dim rListPaste As Range
    8.     Dim ws As Worksheet
    9.    
    10.     For Each ws In Worksheets               'Check for Sheet with name "Owners"
    11.         If ws.name = "Owners" Then
    12.             MsgBox "There is a sheet with the name Owners already.", vbInformation
    13.             Exit Sub
    14.         End If
    15.     Next
    16.    
    17.     Sheets.Add After:=Sheets(Sheets.Count)  'Add new sheet
    18.     ActiveSheet.name = "Owners"             'Name it "Owners"
    19.     Range("A1").Select                          'Select cell A1
    20.     ActiveCell.FormulaR1C1 = "Group name:"    'and write Group name in it
    21.     Range("B1").Select                          'Select cell B1
    22.     ActiveCell.FormulaR1C1 = "Owners:"          'and write Owners in it
    23.  
    24.     Rows("1:1").Select                      'Select top Row and change font properties
    25.     With Selection.Font
    26.         .name = "Arial"
    27.         .Size = 14
    28.         .Bold = True
    29.         .Strikethrough = False
    30.         .Superscript = False
    31.         .Subscript = False
    32.         .OutlineFont = False
    33.         .Shadow = False
    34.         .Underline = xlUnderlineStyleSingle
    35.         .ColorIndex = xlAutomatic
    36.     End With
    37.    
    38.     Columns("B:B").ColumnWidth = 25         'Change Column widths
    39.     Columns("A:A").ColumnWidth = 25
    40.  
    41.     On Error Resume Next
    42.     Worksheets("Owners").Activate           'Activate sheet "Owners"
    43.     Set rListPaste = Range("A2")            'rListPaste = A2 at sheet Owners
    44.    
    45.     Worksheets("Data").Activate             'Select the Data sheet and paste the unique list
    46.    
    47.     Range("D2", Range("D2").End(xlDown)).AdvancedFilter _
    48.     Action:=xlFilterCopy, CopyToRange:=Worksheets("Owners").Range("A2"), Unique:=True
    49.  
    50. End Sub

    I guess this is a quite massive problem to bring up in a forum, but all tips, ideas and help are greatly appreciated.

    Best regards.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Creating unique list and writing to files

    VB Code:
    1. Private Sub CmdButGenExtFiles_Click()
    2. Open "Group1.ext" For Output As 1
    3. Print #1, "Names"
    4. For i = 2 To Sheet1.UsedRange.Rows.Count
    5.     If Sheet1.Cells(i, 4) = "group1" Then Print #1, Sheet1.Cells(i, 1)
    6.    
    7. Next
    8. Close 1
    9. End Sub
    should write your text file

    i don't think you can do what you are trying to do with filters as you want to get part of cells value (each owner), then add to list of owners for each group

    i would change the code from and including on error resume next

    VB Code:
    1. ' don't use    On Error Resume Next
    2.     Worksheets("Owners").Activate           'Activate sheet "Owners"
    3. ' not used    Set rListPaste = Range("A2")            'rListPaste = A2 at sheet Owners
    4.    
    5.     Worksheets("Data").Activate             'Select the Data sheet and paste the unique list
    6.    
    7.     Range("D2", Range("D2").End(xlDown)).AdvancedFilter _
    8.     Action:=xlFilterCopy, CopyToRange:=Worksheets("Owners").Range("A2"), Unique:=True
    9.  
    10.  
    11.    
    12.    
    13.    
    14.     With Worksheets("data")
    15.         For i = 2 To .UsedRange.Rows.Count
    16.             olistarr = split(.Cells(i, 5), ";")
    17.                 For o = 2 To Worksheets("Owners").UsedRange.Rows.Count
    18.                     If Worksheets("Owners").Cells(o, 1) = .Cells(i, 4) Then
    19.                         owns = Worksheets("Owners").Cells(o, 2)
    20.                         For j = 0 To UBound(olistarr)
    21.                             If InStr(1, owns, olistarr(j)) = 0 Then owns = owns & ";" & olistarr(j)
    22.                            
    23.                         Next j
    24.                         Worksheets("Owners").Cells(o, 2) = owns
    25.                         Exit For
    26.                     End If
    27.                 Next o
    28.         Next i
    29.     End With
    this code is not tested as i only have office 97 here and split don't work (VBA5)

    pete

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Creating unique list and writing to files

    i have tested it now, seems that the unique key does not stop duplicate records, my answer would be to write code to do the same job (i know progamming better than excel functions), similar to the code for the second part, which seems to work ok

    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    2

    Resolved Re: Creating unique list and writing to files

    Thanks!

    This solved the problem. I had to do some work to get everything to work exactly as I wanted but your help was without doubt a great contribution to solve the problem (and helping me getting a grip of the syntax)!

    What would the world look like without For, If and While?

    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width