VBA to get last instance of a value, the contents of another cell, and make a new worksheet

GMC111568

New Member
Joined
Oct 3, 2016
Messages
11
I have an excel sheet similar to the following:

ColumnA ColumnB ColumnC
George 1 14
Edith 1 16
Archie 1 26
Betty 2 32
Fred 2 34
Wilma 3 38
Pebbles 4 40
Louise 4 42
Helen 4 44
Florence 4 46

I'd like to create a macro, that when run will create a new sheet, and it will be a list of the last instance of each value in column b, along with the value of column c from that same row.

So, the resulting worksheet would have the following data in it after the macro is run:

ColumnA ColumnB ColumnC
Archie 1 26
Fred 2 34
Wilma 3 38
Florence 4 46

Any suggestions on how to proceed?

Thanks in advance!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Can you explain why the data would change in your example? In that, what tells the macro those are the items you want copied? And do you simply want it to take a snapshot of the current data, so that when you change it, there is still a record of what it was? If yes, would the previous data always be overwritten, or just create an infinite log?
 
Upvote 0
this will give you the last instance of column B values, but in reverse order of how they are listed.

Code:
Sub t()
Dim i As Long, newSh As Worksheet, sh As Worksheet
Set sh = ActiveSheet
Set newSh = Sheets.Add(After:=ActiveSheet)
    With sh
        For i = .Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
            If .Cells(i, 2) <> .Cells(i + 1, 2) And .Cells(i, 2) = .Cells(i - 1, 2) Then
                .Cells(i, 1).Resize(1, 3).Copy newSh.Cells(Rows.Count, 1).End(xlUp)(2)
            End If
        Next
    End With
End Sub
 
Upvote 0
Can you explain why the data would change in your example? In that, what tells the macro those are the items you want copied? And do you simply want it to take a snapshot of the current data, so that when you change it, there is still a record of what it was? If yes, would the previous data always be overwritten, or just create an infinite log?

The data in column B is a group number.

I need to get a spreadsheet that contains information from the last row of each group (the values from Column B - the group itself - as well as Column C).

Again, I only need it for the last row of each group.

In this instance, the data in the spreadsheet itself is generated by another program, and it will always be in this specific order (by group number).

Thanks.
 
Upvote 0
The data in column B is a group number.

I need to get a spreadsheet that contains information from the last row of each group (the values from Column B - the group itself - as well as Column C).

Again, I only need it for the last row of each group.

In this instance, the data in the spreadsheet itself is generated by another program, and it will always be in this specific order (by group number).

Thanks.

Try the code in post #3. It does what you want. You can do a quick sort to put it in the original order if needed. I did not include the sort in the code because there was no information in the original post of how the data would be needed on the output.
 
Upvote 0
This will copy it over in the same order.

Code:
Sub t()
Dim i As Long, newSh As Worksheet, sh As Worksheet
Set sh = ActiveSheet
Set newSh = Sheets.Add(After:=ActiveSheet)
    With sh
        For i = .Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
            If .Cells(i, 2) <> .Cells(i + 1, 2) And .Cells(i, 2) = .Cells(i - 1, 2) Then
                .Rows(i).Copy
                newSh.Cells(2, 1).EntireRow.Insert
            End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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