Extract All lines cotaining the last distinct value.

Alegzandrion

New Member
Joined
Feb 5, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi, I am currently relatively new to excel and I have an issue. I want to extract from a sheet all the lines that contain the last distinct value of a column.

So have something like this
534 16 14
534 64 16
020 24 22
020 16 14
020 64 16
134 24 0
134 8 12
134 8 7
134 15 8
134 15 9

And I would Like to make a macro that would extract for me based on the first column only the bolded lines. After extraction the table should look like this :
534 64 16
020 64 16
134 15 9


Can anyone give me a helping hand ? I tried using lookup, but that only seems to work by entering a specific value.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to Mr. Excel,

I assume that the example you posted in three columns correct? you can use XL2BB in any future posts to paste your exact data.
So you are wanting to extract the last unique value in column A, but the entire row? and what do you mean by "extract"? (a new sheet, replace the current table, etc)
 
Upvote 0
Try this:
I put the result in col E.
VBA Code:
Sub a1123065a()

Dim i As Long, j As Long, k As Long
Dim va
Application.ScreenUpdating = False
va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
For i = 1 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1:    k = k + 1
    Range("E" & k).Resize(1, 3).Value = Range("A" & i).Resize(1, 3).Value
Next
Application.ScreenUpdating = True
End Sub

Example:
Book1
ABCDEFG
153416145346416
25346416206416
3202422134159
4201614
5206416
6134240
7134812
813487
9134158
10134159
Sheet11
 
Upvote 0
Welcome to Mr. Excel,

I assume that the example you posted in three columns correct? you can use XL2BB in any future posts to paste your exact data.
So you are wanting to extract the last unique valuge in column A, but the entire row? and what do you mean by "extract"? (a new sheet, replace the current table, etc)
I did not paste the entire sheet as it is very big. I want to create in another sheet a table based on all the lines of another table, by extracting every ROW containing the last occurence of every distinct value of a column.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
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