+ Reply to Thread
Results 1 to 3 of 3

Macro to compile a list with multiple columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Macro to compile a list with multiple columns

    Hello,
    I have a list which contains three columns (see attached workbook).
    I need a macro to generate a compiled list counting each item and list next to it the corresponding "Length" and "Width".
    It has to be a macro, not formula or pivot table, because this is just a part of a more extensive routine.
    Could anybody advise please?
    Thank you
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: Macro to compile a list with multiple columns

    Sub ben()
        Dim LastRow     As Range, _
            ListItem    As Range, _
            Ndx As Long
            Ndx = 1
        
        Set LastRow = Cells(Rows.Count, "A").End(xlUp)
        
        For Each ListItem In Range("A2", LastRow)
            Err.Clear
            On Error Resume Next
            found = WorksheetFunction.Match(ListItem.Value, Range("E:E"), 0)
            
            If Err.Number > 0 Then  'unique found, so add
                Ndx = Ndx + 1
                Cells(Ndx, "E").Value = ListItem.Value
                Cells(Ndx, "F").Value = WorksheetFunction.CountIf(Range("A2", LastRow), ListItem)
                Cells(Ndx, "G").Resize(columnsize:=2).Value = ListItem.Offset(0, 1).Resize(columnsize:=2).Value
            End If
        Next ListItem
        Range("E1:H1").Value = Array("Item", "Each", "Length", "Width")
    End Sub
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Macro to compile a list with multiple columns

    Here's a quick and dirty solution. It temporarily uses the empty column D to keep track of which items in the list its already accounted for. (I mark the item with an "x" in column D.) Afterwards, I erase it, of course.

    It seems to work as intended. It duplicates your intended output exactly. Modify as needed.

    last_row = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    Row = 1
    
    For x = 2 To last_row
      If Cells(x, 4) <> "x" Then Row = Row + 1
      For y = x To last_row
        If Cells(x, 1) = Cells(y, 1) And Cells(y, 4) <> "x" Then
            Cells(y, 4) = "x"
            Cells(Row, 5) = Cells(x, 1)
            Cells(Row, 6) = Cells(Row, 6) + 1
            Cells(Row, 7) = Cells(x, 2)
            Cells(Row, 8) = Cells(x, 3)
        End If
     Next y
    Next x
    
    Range("D2:D" & last_row).Select
    Selection.ClearContents
    Range("A1").Select

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Compile list of data from same cell in multiple worksheets
    By Abidan in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 01-30-2015, 12:08 PM
  2. Compile multiple columns in one column - error?
    By dim06 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-09-2011, 11:23 AM
  3. compile multiple columns into 1
    By Onestopfanshop in forum Excel General
    Replies: 3
    Last Post: 06-28-2010, 01:06 PM
  4. [SOLVED] Compile list of same cell from multiple worksheets
    By PCakes in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-17-2006, 11:05 AM
  5. macro to compile columns on multiple sheets
    By simonsmith in forum Excel General
    Replies: 2
    Last Post: 05-09-2006, 11:06 AM

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