Macro - Find Last Duplicate And Copy

karldugan

New Member
Joined
May 10, 2016
Messages
47
Hi Excellers,

Need a little help - In column G I have duplicate text, what I want to do, is find the last occurrence of the word "Bal" and then copy everything below that row into a new sheet, called P&L.

The macro I have so far is, with the part I am stuck on highlighted in bold and italics;

Dim newsheet
Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
newsheet.Name = "P&L"
Sheets("Data").Range("I:J,L:M,O:Z").EntireColumn.Delete
Worksheets("Data").Range("1:1").Copy Worksheets("P&L").Range("1:1")
Worksheets("Data").Select
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("G:G"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("A:Z")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With ActiveWorkbook.ActiveSheet
Dim Last_B_RowNumber As Long
Last_B_RowNumber = .Range("G:G").Find(What:="BAL", After:=[G1], _
SearchDirection:=xlPrevious).Row
End With
Rows(ActiveCell.Row & ":" & Rows.Count).Cut Worksheets("P&L").Range("2:2")
Worksheets("Data").Select
ActiveSheet.Name = "Bal"

End Sub


Thanks,

Karl
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Gallen,

When I step through the macro, it is selecting the whole sheet and not the range.

Thanks,

Karl
 
Upvote 0
Try
Code:
Rows(Last_B_RowNumber & ":" & Rows.count).Cut Worksheets("P&L").Range("A2")
 
Last edited:
Upvote 0
Hi Fluff - That does 99.9% of it thanks :D - How do I get it to drop down one row though? So at the moment it copies the last row with Bal in, I want the first row after that.

Again, thanks for sorting this for me :)

Karl.
 
Upvote 0
Simply add one to the first row
Code:
Last_B_RowNumber +1 &...
 
Upvote 0
I should have read further. The bold code was fine :D

Good to see it's fixed
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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