VBA - combining rows
Results 1 to 4 of 4

Thread: VBA - combining rows

  1. #1
    New Member
    Join Date
    Aug 2019
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question VBA - combining rows

    Hi guys

    Probably quite a simple fix for someone who knows VB.

    I found a really useful macro (thank you hiker95) on this site for combining multiple rows with a unique identifier. It's configured to combine the first 2 columns of each row after the unique ID in column 1.

    All I need it to do is combine the first 3 columns after the unique ID instead of just 2. I just wondered if someone could help amend the script for me to achieve this?

    Any help would be massively appreciated



    Code is:

    Option Explicit
    Sub ReorgDataV2()
    ' hiker95, 02/16/2014, ME758017
    Dim r As Long, lr As Long, n As Long, rr As Long, nc As Long
    Application.ScreenUpdating = False
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For r = 1 To lr
    n = Application.CountIf(Columns(1), Cells(r, 1).Value)
    If n > 1 Then
    For rr = r + 1 To r + n - 1
    nc = Cells(r, Columns.Count).End(xlToLeft).Column + 1
    Cells(r, nc).Resize(, 2).Value = Cells(rr, 2).Resize(, 2).Value
    Cells(rr, 1).Resize(, 3).ClearContents
    Next rr
    End If
    r = r + n - 1
    Next r
    On Error Resume Next
    Range("A1:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    Columns.AutoFit
    Application.ScreenUpdating = True
    End Sub

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,976
    Post Thanks / Like
    Mentioned
    459 Post(s)
    Tagged
    45 Thread(s)

    Default Re: VBA - combining rows

    Hi & welcome to MrExcel.
    Try changing this line
    Code:
    Cells(r, nc).Resize(, 2).Value = Cells(rr, 2).Resize(, 2).Value
    to
    Code:
    Cells(r, nc).Resize(, 3).Value = Cells(rr, 2).Resize(, 3).Value
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    New Member
    Join Date
    Aug 2019
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Re: VBA - combining rows

    Quote Originally Posted by Fluff View Post
    Hi & welcome to MrExcel.
    Try changing this line
    Code:
    Cells(r, nc).Resize(, 2).Value = Cells(rr, 2).Resize(, 2).Value
    to
    Code:
    Cells(r, nc).Resize(, 3).Value = Cells(rr, 2).Resize(, 3).Value

    Thanks so much that worked perfectly.

    Massively appreciate your help.

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,976
    Post Thanks / Like
    Mentioned
    459 Post(s)
    Tagged
    45 Thread(s)

    Default Re: VBA - combining rows

    You're welcome & thanks for the feedback
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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