Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

VBA: Concatenate and replace rows to single row based on min/max dates?

I have a spreadsheet that lists people staying at hotels over date ranges. I'd like to combine the people who have stayed at the same hotel into a single row, displaying the oldest date in one column, and the newest date in the second column. I consider it a form of "nesting."

In my head, the macro will select a range that includes all rows with the same hotel, checks and replaces the dates with the oldest/newest in their respective columns, then concatenates the names of the people into a single cell separated by semicolons.

This would need to be a macro, since it's part of a larger workflow, and the data is frequently imported from a separate report.

Any help would be greatly appreciated!!

Picture of the data here.

Edit: Here's what I have so far to concatenate the names from duplicate hotel lines:

Sub Nest()

Dim hotel As Range
Dim person As Range
Dim persa As String
Dim persb As String
Dim persc As String
Dim persd As String
Dim perse As String
Dim combine As String

For Each hotel In Range("A2:A19")

Set person = hotel.Offset(0, 3)

    If hotel.Value = hotel.Offset(1, 0) And hotel.Value = hotel.Offset(2, 0) And hotel.Value = hotel.Offset(3, 0) And hotel.Value = hotel.Offset(4, 0) Then
        persa = person.Value
        persb = person.Offset(1, 0).Value
        persc = person.Offset(2, 0).Value
        persd = person.Offset(3, 0).Value
        perse = person.Offset(4, 0).Value
        combine = persa & "; " & persb & ";" & persc & "; " & persd & "; " & perse
        person = combine
            Else
                If hotel.Value = hotel.Offset(1, 0) And hotel.Value = hotel.Offset(2, 0) And hotel.Value = hotel.Offset(3, 0) Then
                persa = person.Value
                persb = person.Offset(1, 0).Value
                persc = person.Offset(2, 0).Value
                persd = person.Offset(3, 0).Value
                combine = persa & "; " & persb & "; " & persc & "; " & persd
                person = combine
                    Else
                        If hotel.Value = hotel.Offset(1, 0) And hotel.Value = hotel.Offset(2, 0) Then
                        persa = person.Value
                        persb = person.Offset(1, 0).Value
                        persc = person.Offset(2, 0).Value
                        combine = persa & "; " & persb & ";" & persc
                        person = combine
                            Else
                                If hotel.Value = hotel.Offset(1, 0) Then
                                persa = person.Value
                                persb = person.Offset(1, 0).Value
                                combine = persa & "; " & persb
                                person = combine
                            End If
                        End If
                    End If
                End If


Next hotel

End Sub

It's pretty roundabout, and if there are any more than 5 duplicate hotels, then I'd have to keep adding if/else lines...

Next up is replacing the oldest date in Checkin and the newest date in Checkout...

Comments