Macro - Find Last Duplicate And Copy

karldugan

New Member
Joined
May 10, 2016
Messages
19
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
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,185
Office Version
  1. 2016
Platform
  1. Windows
.
 
Last edited:

karldugan

New Member
Joined
May 10, 2016
Messages
19
Hi Gallen,

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

Thanks,

Karl
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,139
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Try
Code:
Rows(Last_B_RowNumber & ":" & Rows.count).Cut Worksheets("P&L").Range("A2")
 
Last edited:

karldugan

New Member
Joined
May 10, 2016
Messages
19
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,139
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Simply add one to the first row
Code:
Last_B_RowNumber +1 &...
 

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
2,006
I should have read further. The bold code was fine :D

Good to see it's fixed
 

Watch MrExcel Video

Forum statistics

Threads
1,122,964
Messages
5,599,071
Members
414,281
Latest member
Engjamal2021

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
Top