+ Reply to Thread
Results 1 to 5 of 5

VBA Grouping Concatenate best way to handle, based on data in column

Hybrid View

PeeBee VBA Grouping Concatenate best... 04-18-2016, 03:39 AM
gbeats101 Re: VBA Grouping Concatenate... 04-18-2016, 04:07 AM
LordLoki Re: VBA Grouping Concatenate... 04-18-2016, 04:35 AM
PeeBee Re: VBA Grouping Concatenate... 04-18-2016, 06:19 AM
gbeats101 Re: VBA Grouping Concatenate... 04-18-2016, 05:27 AM
  1. #1
    Registered User
    Join Date
    09-27-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    VBA Grouping Concatenate best way to handle, based on data in column

    Hi all,
    I have been working on a grouping of data in Column A based on data in column D. if the data in Column D matches next row in D then Concatenate data in column A and delete that line

    any help appreciated to steer me in right direction
    thanks regards Peter

    Data Sample.jpg




    My code so far

    Sub CommandButton2_Click()
    Dim x               As Integer
    Dim MyTimer         As Double
    x = MsgBox("This with now Group all Data IN Column A and Sheet1!", vbOKCancel)
    If x = 2 Then Exit Sub
    If x = 1 Then
    MsgBox (" Application.EnableEvents = False")
     'Application.EnableEvents = False
     
         Dim Lastrow As Long, r As Long
        Application.ScreenUpdating = False
        Lastrow = Range("A" & Rows.Count).End(xlUp).Row
      
      'status bar code loop
       ' For j = 1 To 50
        
        For r = Lastrow To 2 Step -1
            If Range("D" & r).Value = Range("D" & r - 1).Value Then
                Range("A" & r).Value = Range("A" & r - 1).Value & "_" & Range("A" & r).Value
                Rows(r).Delete xlShiftUp
            
            End If
            'status bar code line under
            ' Application.StatusBar = "Progress: " & r & " of " & Lastrow & ": " & Format(r / Lastrow, "0%")
        Next r
     
        ActiveSheet.UsedRange.Columns.AutoFit
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.StatusBar = False
     
     
     End If
     
     End Sub
    Attached Images Attached Images

  2. #2
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: VBA Grouping Concatenate best way to handle, based on data in column

    Hi, when you want to loop through rows and delete rows at the same time you need to do it slightly differently because deleting a row will cause your loop to be off by a row every time you delete one.

    you can follow it and adjust with a variable using a for loop but i like to just a while loop because its easier to follow.
    obviously using your current method of getting the last row as a marker to the end of the table we will keep count of the deleted rows to offset this value

    Public Sub Test()
        Dim Rw As Long
        Dim DeletedRws As Long
    
    Rw = 1
    DeletedRws = 0
    
    'Start on row 1, if we delete a row we will just append to the next row if its a match and delete the current row
    'we dont actually need to include the last row so end before we hit that row
    While Rw < LastRow - DeletedRws
            If Range("D" & Rw).Value = Range("D" & Rw + 1).Value Then
                Range("A" & Rw + 1).Value = Range("A" & Rw).Value & "_" & Range("A" & Rw + 1).Value
                Rows(Rw).Delete
                DeletedRws = DeletedRws + 1
            Else
                Rw = Rw + 1
            End If
            DoEvents
    Wend
    End Sub
    just add the code here to your own, and comment out your entire loop or delete it....

    i didnt add the progress bar code here

    remove the do events if you like, i always keep it in there while testing....... and even after just do something like 'If Rw MOD 1000 = 0 then doevents' so you can at least get a change to recover from an infinate loop................ but this loop should be fine....
    Last edited by gbeats101; 04-18-2016 at 04:10 AM.

  3. #3
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: VBA Grouping Concatenate best way to handle, based on data in column

    Actually your way to loop through the range is correct and does not need Change when you start from the Bottom you do not screw up the process by deleteing a row.
    Your code is perfectly fine you just delete the wrong row :D



    it needs to be
     Rows(r-1).Delete xlShiftUp
    Sub CommandButton2_Click()
    Dim x               As Integer
    Dim MyTimer         As Double
    x = MsgBox("This with now Group all Data IN Column A and Sheet1!", vbOKCancel)
    If x = 2 Then Exit Sub
    If x = 1 Then
    MsgBox (" Application.EnableEvents = False")
     'Application.EnableEvents = False
     
         Dim Lastrow As Long, r As Long
        Application.ScreenUpdating = False
        Lastrow = Range("A" & Rows.Count).End(xlUp).Row
      
      'status bar code loop
       ' For j = 1 To 50
        
        For r = Lastrow To 2 Step -1
            If Range("D" & r).Value = Range("D" & r - 1).Value Then
                Range("A" & r).Value = Range("A" & r - 1).Value & "_" & Range("A" & r).Value
                Rows(r-1).Delete xlShiftUp
            
            End If
            'status bar code line under
            ' Application.StatusBar = "Progress: " & r & " of " & Lastrow & ": " & Format(r / Lastrow, "0%")
        Next r
     
        ActiveSheet.UsedRange.Columns.AutoFit
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.StatusBar = False
     
     
     End If
     
     End Sub
    Edit: Also i wanna mention that the initial code with the for loop performs around 33% better than the solution with the while loop. with a small data set that is not really noticable but in a dataset with 50k rows for example the for loop is running through in 2 minutes and the while loop needs 3 minutes which is a big difference.

    You could speed up the process even more by reading everything into an array loop through that and only write back the values that you need. but unless you have a massive amount of Data that would not make much noticable difference.
    Last edited by LordLoki; 04-18-2016 at 05:30 AM.

  4. #4
    Registered User
    Join Date
    09-27-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA Grouping Concatenate best way to handle, based on data in column

    Quote Originally Posted by LordLoki View Post
    Actually your way to loop through the range is correct and does not need Change when you start from the Bottom you do not screw up the process by deleteing a row.
    Your code is perfectly fine you just delete the wrong row :D



    it needs to be
     Rows(r-1).Delete xlShiftUp
    Sub CommandButton2_Click()
    Dim x               As Integer
    Dim MyTimer         As Double
    x = MsgBox("This with now Group all Data IN Column A and Sheet1!", vbOKCancel)
    If x = 2 Then Exit Sub
    If x = 1 Then
    MsgBox (" Application.EnableEvents = False")
     'Application.EnableEvents = False
     
         Dim Lastrow As Long, r As Long
        Application.ScreenUpdating = False
        Lastrow = Range("A" & Rows.Count).End(xlUp).Row
      
      'status bar code loop
       ' For j = 1 To 50
        
        For r = Lastrow To 2 Step -1
            If Range("D" & r).Value = Range("D" & r - 1).Value Then
                Range("A" & r).Value = Range("A" & r - 1).Value & "_" & Range("A" & r).Value
                Rows(r-1).Delete xlShiftUp
            
            End If
            'status bar code line under
            ' Application.StatusBar = "Progress: " & r & " of " & Lastrow & ": " & Format(r / Lastrow, "0%")
        Next r
     
        ActiveSheet.UsedRange.Columns.AutoFit
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.StatusBar = False
     
     
     End If
     
     End Sub
    Edit: Also i wanna mention that the initial code with the for loop performs around 33% better than the solution with the while loop. with a small data set that is not really noticable but in a dataset with 50k rows for example the for loop is running through in 2 minutes and the while loop needs 3 minutes which is a big difference.

    You could speed up the process even more by reading everything into an array loop through that and only write back the values that you need. but unless you have a massive amount of Data that would not make much noticable difference.
    Hi LordLoki,

    Doh! thanks and yes that works, what is the best way to ignore the blank cells as this code looks at these as a duplicate but I don't want these to be grouped

    thanks also for your info about performance

    regards Peter

  5. #5
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: VBA Grouping Concatenate best way to handle, based on data in column

    Hey, your right haha , but i like to move forward

+ 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. Concatenate Data based on column
    By dylanvv in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-15-2015, 05:34 PM
  2. Concatenate column of data based on interation value in other column
    By wheel1 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-11-2015, 07:32 PM
  3. [SOLVED] VBA macro Code for grouping based on column A
    By Dharani Suresh in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-05-2014, 04:41 AM
  4. [SOLVED] Concatenate row data in columns B-F based on duplicate value in column A
    By wolffia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2014, 10:03 AM
  5. Replies: 9
    Last Post: 12-13-2013, 07:40 PM
  6. Grouping into month based the data and inserting column
    By thisisgerald in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-07-2012, 06:35 AM
  7. Help! Grouping amounts in one column based on id on another column
    By explorart in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-17-2006, 04:12 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