VBA Script Help

BBellers

New Member
Joined
Aug 6, 2021
Messages
3
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hi - I need help perfecting my VBA Script for a fairly simple macro.

I have duplicated names in column A, with unique values in column H. I would like to concatenate the values in column H, if column A is the same. And then delete duplicated rows, but have the coordinating concatenate value stay with the respected row.

What I have so far will concatenate the values, but lists them back to back in column I, making it difficult to then delete the duplicate rows. I have attached an image of what this script creates.


Sub ConcatenateCellsIfSameValueExists()
DestRowRef = 2
CheckedCell = Cells(2, "A").Value
For I = 2 To Range("A" & Rows.Count).End(xlUp).Row + 1
If Cells(I, "A").Value <> CheckedCell Then
tempConValues = CheckedCell & " " & tempConValues
Cells(DestRowRef, "I").Value = tempConValues
tempConValues = ""
DestRowRef = DestRowRef + 1
End If
tempConValues = tempConValues & ", " & Cells(I, "H").Value
CheckedCell = Cells(I, "A").Value
Next
End Sub

vba.PNG
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub BBellers()
   Dim Cl As Range
   
   With CreateObject("Scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl.Row
            Cl.Offset(, 8).Value = Cl.Value & ", " & Cl.Offset(, 7).Value
         Else
            Cells(.Item(Cl.Value), 9).Value = Cells(.Item(Cl.Value), 9).Value & ", " & Cl.Offset(, 7).Value
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Awesome! ok that worked... but now I realized I need to concatenate for column F instead of H... and is there a way to delete the duplicated row after placing the concatenate value into the row?
 
Upvote 0
Ok, how about
VBA Code:
Sub BBellers()
   Dim Cl As Range
   
   With CreateObject("Scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl.Row
            Cl.Offset(, 8).Value = Cl.Value & ", " & Cl.Offset(, 5).Value
         Else
            Cells(.Item(Cl.Value), 9).Value = Cells(.Item(Cl.Value), 9).Value & ", " & Cl.Offset(, 5).Value
         End If
      Next Cl
   End With
   Range("I2:I" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub BBellers()
   Dim Cl As Range
  
   With CreateObject("Scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl.Row
            Cl.Offset(, 8).Value = Cl.Value & ", " & Cl.Offset(, 5).Value
         Else
            Cells(.Item(Cl.Value), 9).Value = Cells(.Item(Cl.Value), 9).Value & ", " & Cl.Offset(, 5).Value
         End If
      Next Cl
   End With
   Range("I2:I" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
Yesss Beautiful. Thank you!!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,298
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top