VBA dynamic concatenate

skinlo

New Member
Joined
Apr 28, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

First of all, my level of VBA knowledge is hacking together things I find on the internet, so I apolgise if this is a stupid question.

I have the following table (made up details, but same format):
ServerPatch NoDate
Server 1127/04/22
Server 1227/04/22
Server 1327/04/22
Server 1425/04/22

I want to turn it into something that looks like this:

ServerPatch NosDate
Server 11, 2, 327/04/22
Server 1425/04/22

Basically I want a way to concatenate based on the date, which could change depending the data coming through (so it could be 20 entries with 5 different dates). Looking online, it seems you have to statically define what you want to be concatenate in VBA., I was wondering if there was a way you could do it dynamically, where it wouldn't matter how long the data is and where you could filter it by date.

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Read the comments, act on those starting with <<<<

VBA Code:
Option Explicit

Sub CondenseTable()
    Dim vIn As Variant, vOut As Variant
    Dim lRi As Long, UB As Long, lRo As Long, lRi2 As Long
    Dim sPatch As String, sServ As String
    Dim dtDate As Date
    
    'read input table into array for fast processing
    vIn = Range("A1").CurrentRegion '<<<< set to top left of table
    UB = UBound(vIn, 1) 'number of rows in table
    
    ReDim vOut(1 To UB, 1 To 3) '<<< assuming 3 columns as per example
    lRo = 1
    'copy header row
    For lRi = 1 To 3
        vOut(1, lRi) = vIn(1, lRi)
    Next lRi
    'now go through input table
    For lRi = 2 To UB   'skip header
        sServ = vIn(lRi, 1)
        If Len(sServ) Then
            sPatch = vIn(lRi, 2)
            dtDate = vIn(lRi, 3)
            For lRi2 = lRi + 1 To UB
                If vIn(lRi2, 1) Like sServ And vIn(lRi2, 3) = dtDate Then
                    sPatch = sPatch & ", " & vIn(lRi2, 2)
                    vIn(lRi2, 1) = ""   'clear server name so won't be processed again
                End If
            Next lRi2
            lRo = lRo + 1
            vOut(lRo, 1) = sServ
            vOut(lRo, 2) = sPatch
            vOut(lRo, 3) = dtDate
            
        End If
    Next lRi
    
    Range("E1").Resize(UB, 3) = vOut    '<<<< set address to where you want your output table to begin
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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