+ Reply to Thread
Results 1 to 9 of 9

Macro to Combine data for Columns with same header

Hybrid View

Pandrade Macro to Combine data for... 04-18-2020, 12:19 PM
Vraag en antwoord Re: Macro to Combine data for... 04-18-2020, 12:25 PM
xladept Re: Macro to Combine data for... 04-18-2020, 12:51 PM
Vraag en antwoord Re: Macro to Combine data for... 04-18-2020, 01:01 PM
Marc L Hi ! Try this ! 04-18-2020, 01:04 PM
Pandrade Re: Macro to Combine data for... 04-18-2020, 01:10 PM
Pandrade Re: Macro to Combine data for... 04-18-2020, 01:46 PM
Marc L Re: Macro to Combine data for... 04-18-2020, 07:04 PM
xladept Re: Macro to Combine data for... 04-18-2020, 02:04 PM
  1. #1
    Registered User
    Join Date
    04-16-2020
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Macro to Combine data for Columns with same header

    Hi,

    I need a macro that will combine the data for the columns with headers that have the same name. In the attached example I would end up with three columns, one for MUN, one for PRT, and one for GAM.

    Thank you for your help.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Macro to Combine data for Columns with same header

    And what is the expected result?

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to Combine data for Columns with same header

    Try this and we can modify it later:

    Sub CombHeads(): Dim r As Long, c As Long
    Dim m As Long, n As Long, wa As Worksheet
    Set wa = ActiveWorkbook.Sheets("Sheet1")
    r = wa.Rows.Find("*", , , , xlByRows, xlPrevious).Row
    c = wa.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
        Cells(1, c + 2).Resize(1, 3) = Split("MUN PRT GAM")
                    For m = 2 To r: For n = 1 To c
                        If Cells(m, n) Then
                    Select Case Cells(1, n)
        Case "MUN": Cells(m, c + 2) = Cells(m, c + 2) + Cells(m, n)
        Case "PRT": Cells(m, c + 3) = Cells(m, c + 3) + Cells(m, n)
        Case "GAM": Cells(m, c + 4) = Cells(m, c + 4) + Cells(m, n)
                    End Select
                        End If: Next n: Next m: End Sub
    Last edited by xladept; 04-18-2020 at 12:54 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Macro to Combine data for Columns with same header

    or dynamic

    Sub VenA()
    ar = Sheet1.Cells(1).CurrentRegion
    ReDim ar1(1, 0)
      For j = 2 To UBound(ar)
        For jj = 1 To UBound(ar, 2)
          If ar(j, jj) <> "" Then
            ar1(0, t) = ar(1, jj)
            ar1(1, t) = ar(j, jj)
            t = t + 1
            ReDim Preserve ar1(1, t)
          End If
        Next jj
      Next j
      With Sheet2
        .Cells.Clear
        .Cells(1).Resize(, 2) = Array("Type", "Value")
        .Cells(1).Offset(1).Resize(t, 2) = Application.Transpose(ar1)
      End With
    End Sub

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    Or the Excel way as a VBA beginner starter :

    PHP Code: 
    Sub Demo1()
            
    Dim C%, P%, S$
        
    With ActiveSheet.UsedRange
            
    For 2 To .Columns.Count
                   P 
    Application.Match(.Cells(C), .Rows(1), 0)
                If 
    C Then
                    Range
    (.Cells(2C), .Cells(C).End(xlDown)).Copy .Cells(P).End(xlDown)(2)
                    
    IIf(""",""") & .Cells(C).Address(00)
                
    End If
            
    Next
        End With
            
    If "" Then Range(S).EntireColumn.Delete
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » ! ◄ ◄

  6. #6
    Registered User
    Join Date
    04-16-2020
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Re: Macro to Combine data for Columns with same header

    Vraag en antwoord,

    Not quite what I need. The data should not be summed just combined into one column per header. The attached file is what the end result should be.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    04-16-2020
    Location
    United States
    MS-Off Ver
    2016
    Posts
    16

    Re: Macro to Combine data for Columns with same header

    The data should stack into one column if the column has the same name as in the attached file in reply #6. Hope someone can help.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Macro to Combine data for Columns with same header


    Quote Originally Posted by Pandrade View Post
    as in the attached file in reply #6. Hope someone can help.
    As you already got a solution in post #5 ‼

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to Combine data for Columns with same header

    Another try:

    Sub CombOnlyHeads(): Dim r As Long, c As Long, X As Range
    Dim m As Long, n As Long, s As Long, t As Long, u As Long
    Dim wa As Worksheet: Set wa = ActiveWorkbook.ActiveSheet
    s = 2: t = 2: u = 2
    
    c = wa.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
        Cells(1, c + 2).Resize(1, 3) = Split("MUN PRT GAM")
                    For n = 1 To c: Set X = wa.UsedRange.Columns(n)
                    m = WorksheetFunction.CountA(X) - 1
                    Select Case Cells(1, n)
        Case "MUN": Cells(s, c + 2).Resize(m, 1).Value = _
            Cells(2, n).Resize(m, 1).Value: s = s + m
        Case "PRT": Cells(t, c + 3).Resize(m, 1).Value = _
            Cells(2, n).Resize(m, 1).Value: t = t + m
        Case "GAM": Cells(u, c + 4).Resize(m, 1).Value = _
            Cells(2, n).Resize(m, 1).Value: u = u + m
                    End Select
                         Next n
                wa.Columns("A:" & Chr(65 + c)).Delete
                        End Sub

+ 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. Find multiple column header, filter out other columns, and export data using a macro
    By trevtrev in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2017, 06:24 PM
  2. [SOLVED] Data Formating - Macro to copy header row and paste to multiple columns.
    By Mysore in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-01-2017, 09:57 AM
  3. [SOLVED] Macro to copy data from all columns containing a specified header to a combined column
    By Mamagregory in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-16-2014, 12:18 PM
  4. Replies: 2
    Last Post: 10-13-2012, 03:30 AM
  5. Replies: 0
    Last Post: 08-02-2012, 10:46 PM
  6. COMBINE data in several WORKSHEETS with same header row
    By Coconuts in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2005, 01:20 PM

Tags for this Thread

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