+ Reply to Thread
Results 1 to 13 of 13

Help with a macro to combine rows in a spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Help with a macro to combine rows in a spreadsheet

    Attached is a sample spreadsheet of what I have and what I want it to look like. Any help would be greatly appreciated to create a macro to run. Also, is there a resource where I can learn to write macros?

    Thanks
    Attached Files Attached Files
    Last edited by theatergirl; 03-21-2013 at 10:20 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Help with a macro to combine rows in a spreadsheet

    On the output section, you have data in column N. I can not follow the logic

  3. #3
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Help with a macro to combine rows in a spreadsheet

    With column N, i just want the content of the cell to go to the next row up if the customer number in column a matches. Does that make sense?

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help with a macro to combine rows in a spreadsheet

    This should do what you want.
    Option Explicit
    
    Sub test()
        Dim a, i As Long, ii As Long, n As Long, t As Long, w
        a = Sheets("sheet1").Cells(1).CurrentRegion.Value
        t = UBound(a, 2)
        ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 10)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 1 To UBound(a, 1)
                If Not .exists(a(i, 1)) Then
                    n = n + 1: .Item(a(i, 1)) = VBA.Array(n, t)
                    For ii = 1 To UBound(a, 2)
                        a(n, ii) = a(i, ii)
                    Next
                Else
                    w = .Item(a(i, 1))
                    w(1) = w(1) + 1
                    If w(1) > UBound(a, 2) Then
                        ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 10)
                    End If
                    a(w(0), w(1)) = a(i, t)
                    .Item(a(i, 1)) = w
                End If
            Next
        End With
        With Sheets.Add().Cells(1).Resize(n, UBound(a, 2))
            .Value = a
            .Columns.AutoFit
        End With
    End Sub

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Help with a macro to combine rows in a spreadsheet

    Jindon has provided you a solution!

  6. #6
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Help with a macro to combine rows in a spreadsheet

    when i try to run it, i get an error that says Run-time error '9': Subscript out of range

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Help with a macro to combine rows in a spreadsheet

    When I run it, I did not get any error. In which line does the error highlight with yellow color error?

  8. #8
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Help with a macro to combine rows in a spreadsheet

    this is what i get. sorry i am not good with these
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Help with a macro to combine rows in a spreadsheet

    Okay!
    In your original data you have sheet1. You need to change the sheet name from sheet1 in to your actual sheet name. You do not have to worry about the result as a new sheet name is added. What is the name of the sheet you now have, if you struggle I can amend it for you? Just change the sheet name and run it and will see the result on new added sheet.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help with a macro to combine rows in a spreadsheet

    Change
    a = Sheets("sheet1").Cells(1).CurrentRegion.Value
    to
    a = Cells(1).CurrentRegion.Value

  11. #11
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Help with a macro to combine rows in a spreadsheet

    Now I get Run-time error '1004': Application-defined or object-defined error and it creates a new sheet each time I try to run it.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help with a macro to combine rows in a spreadsheet

    .............
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    10-24-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Help with a macro to combine rows in a spreadsheet

    I was able to put my spreadsheet into this one and get it to work! thank you so much for your help and patience!

+ 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