+ Reply to Thread
Results 1 to 8 of 8

Collapsing Repeated Data

Hybrid View

teddybouch Collapsing Repeated Data 07-09-2008, 11:08 AM
TheNorm You could try making a Pivot... 07-09-2008, 11:48 AM
teddybouch Thanks for the tip! I had... 07-09-2008, 01:10 PM
TheNorm I tried a Pivot Table for... 07-10-2008, 08:57 AM
teddybouch No worries - I was glad to... 07-10-2008, 12:16 PM
teddybouch Okay - progress has been... 07-11-2008, 09:09 AM
teddybouch Alright, I'm almost there and... 07-11-2008, 02:31 PM
teddybouch Alright, apparently it's some... 07-11-2008, 04:24 PM
  1. #1
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    No worries - I was glad to use pivot tables anyway, and it gave me some useful information. The approach that I'm taking now is using formulas. Namely,

    IF(INDEX(Database, MATCH($A2, instruments!$B$1:$B$43483, 0), 3) = Sheet2!B$1, INDEX(Database, MATCH($A2, instruments!$B$1:$B$43483, 0), 1), " ")

    Where Database is the whole sheet of original data, with the vendor part number in the first column, our part number in the second column, and the vendor name in the third column. This formula goes into the fields that would have been the data in the pivot table - I simply copied the column and row headers. This means that vendor names are in the topmost row (all cells ending in 1) and our part numbers in the leftmost column (all cells beginning with A).

    As I understand it, the first match statement returns for a given our part number what row that number is in. If the vendor name in that row is equal to the column header, then it populates the cell with the vendor's part number. Otherwise, it fills with an empty string.

    The problem that I am now running into is that this will only work for the first time that it finds a given vendor-our part number combination. Don't ask me why, but some vendors have multiple different part numbers that correspond to our one. What I would like to do is to create additional columns for the vendors for which this is true, then modify the above formula to return the second, third, fourth, etc. result rather than the first. The pivot table tells me the number of times I have to do this for each vendor, so I know how many columns to do already.

    Thoughts? Thanks for all the help so far!

  2. #2
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    Okay - progress has been made. A fine gentleman in the IT department threw together a VB script that successfully populated the interior of what would have been the Pivot Table (if the tool had worked as desired) with the list of all part numbers that correspond to that vendor and internal part number. Here's his code:

    Sub buildTable()
    Dim instrumentsSheet As Worksheet
    Dim sheet1Sheet As Worksheet
    Dim targetCell As Range


    Application.ScreenUpdating = False
    Set instrumentsSheet = ThisWorkbook.Sheets("instruments")
    Set sheet1Sheet = ThisWorkbook.Sheets("Sheet1")
    On Error GoTo what

    For i = 2 To 43484
    If WorksheetFunction.IsNA(instrumentsSheet.Cells(i, 4).Value) = False And WorksheetFunction.IsNA(instrumentsSheet.Cells(i, 5).Value) = False Then
    Set targetCell = sheet1Sheet.Cells(instrumentsSheet.Cells(i, 4).Value, instrumentsSheet.Cells(i, 5).Value)
    If Len(targetCell.Value) = 0 Then
    targetCell.Value = instrumentsSheet.Cells(i, 1).Value
    Else
    targetCell.Value = targetCell.Value & ", " & instrumentsSheet.Cells(i, 1).Value
    End If

    End If
    If i Mod 100 = 0 Then Debug.Print i




    Next i

    Application.ScreenUpdating = True
    what:
    Debug.Print i

    End Sub
    Note that the sheet "instruments" contains the following columns: vendor part number, internal part number, vendor name, row in the resulting table corresponding to the internal part number, column in the resulting table corresponding to the vendor name.

    Now, if I am to do exactly what was asked of me, I now need to modify this code to split the cells with multiple entries into new columns. For instance,

    ------------| VendorName
    Internal P/N | MJK2332, VEN87509

    Becomes

    ------------| VendorName1 | VendorName2
    Internal P/N | __MJK2332__ | VEN87509

    Does anyone know how to do this? I'm not really well-versed in VBScript for Excel. Are there cheat sheets anywhere of syntax that could help me learn what commands exist?
    Last edited by teddybouch; 07-11-2008 at 09:14 AM.

  3. #3
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    Alright, I'm almost there and every part of it has worked at some point. However, the constantly finicky part of code is that for inserting a column. Initially, I used this:

    targetCell.Select
    MsgBox "Target Cell Selected", 0, "Got here"
    Selection.EntireColumn.Insert
    MsgBox "Column Inserted", 0, "Got here"
    That worked great for a little while, then after working on some other parts of code, it changed its mind and for some reason decided not to like that syntax. It would select the correct cell, but wouldn't do the column insertion. In desperation, I tried a little re-ordering.

    targetCell.EntireColumn.Select
    MsgBox "Target Cell Selected", 0, "Got here"
    Selection.Insert
    MsgBox "Column Inserted", 0, "Got here"
    This inexplicably worked, but I wasn't asking any questions. I kept working, and now it doesn't like either form, nor will it take

    targetCell.EntireColumn.Insert
    Here is all the code in its current form together:

    Sub buildTable()
        Dim instrumentsSheet As Worksheet
        Dim sheet1Sheet As Worksheet
        Dim pivotSheet As Worksheet
        Dim targetCell As Range
        
        Application.ScreenUpdating = False
        Set instrumentsSheet = ThisWorkbook.Sheets("instruments")
        Set sheet1Sheet = ThisWorkbook.Sheets("Sheet1")
        Set pivotSheet = ThisWorkbook.Sheets("pivot")
        On Error GoTo what
        
        'Delete the existing data in all columns so that the macro can be run without
        '   creating extra columns accidentally
        sheet1Sheet.Range("B1:IV8155").Select
        Selection.Value = ""
        
        'Replace vendor headers
        Set targetCell = sheet1Sheet.Cells(1, 2)
        pivotSheet.Range("B1:CX1").Copy Destination:=targetCell
        
        'Go through and create all needed columns for every vendor (2 through 102)
        For i = 2 To 102
        
            'Parse through the maximum number of multiple entries for each vendor
            Vendor = pivotSheet.Cells(1, i).Value
            VendorMax = pivotSheet.Cells(2, i).Value
            
            'Create that number of columns for the vendor
            If (VendorMax > 1) Then
                For j = 1 To (VendorMax - 1)
            
                    'Select the first row in that column
                    VendorCol = Application.Match(Vendor, sheet1Sheet.Range("A1:IV1"))
                    Set targetCell = sheet1Sheet.Cells(1, VendorCol + j)
                
                    'Insert the column
                    targetCell.EntireColumn.Insert
                    MsgBox "Column selected", 0, "Got here"
                    'Selection.Insert
                    MsgBox "Column inserted", 0, "Got here"
                    
                    'Title the column
                    Set targetCell = sheet1Sheet.Cells(1, VendorCol + j)
                    targetCell.Value = Vendor & (j + 1)
                
                Next j
            End If
        Next i
        
        'Go through the vendor item numbers (2 to 38346)
        For i = 2 To 38346
        
            'Get the row that the internal part number is in
            PartRow = instrumentsSheet.Cells(i, 4).Value
            MsgBox PartRow, 0, "PartRow"
            
            'For each vendor item number, make sure that an internal part number exists
            If WorksheetFunction.IsNA(PartRow) = False Then
                
                'Set the target cell as the leftmost cell for the vendor in the internal part
                '   number row
                Vendor = instrumentsSheet.Cells(i, 3).Value
                'MsgBox Vendor, 0, "Vendor"
                VendorCol = Application.Match(Vendor, sheet1Sheet.Range("A1:IV1"))
                'MsgBox VendorCol, 0, "VendorCol"
                Set targetCell = sheet1Sheet.Cells(PartRow, VendorCol)
                
                'While the target cell has something in it, move the target cell one to the right
                Do While ((Len(targetCell.Value) = 0) = False)
                    VendorCol = VendorCol + 1
                    Set targetCell = sheet1Sheet.Cells(PartRow, VendorCol)
                Loop
                
                'Put the vendor part number in the target cell
                targetCell.Value = instrumentsSheet.Cells(i, 1).Value
                
            End If
            If i Mod 100 = 0 Then Debug.Print i
    
        Next i
    
        Application.ScreenUpdating = True
    what:
        Debug.Print i
        
    End Sub
    If anyone has any ideas as to what the deuce is going on here, I would really appreciate some help. I just learned to program Excel macros yesterday, and this problem is really confusing the heck out of me.

  4. #4
    Registered User
    Join Date
    07-09-2008
    Location
    Panama City, FL
    MS-Off Ver
    2007
    Posts
    42
    Alright, apparently it's some odd formatting error that was caused by my clearing all of the cells in the first portion by setting their value to empty string. By using the Delete method instead, it works as desired.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1