VBA - Problems finding second last row and copying text

Togora

New Member
Joined
Dec 1, 2018
Messages
21
Hi All,

I have this code which adds blank rows at points on a table where names change over from one to the next.

HTML:
Bob
Bob
      New blank row here
Anne
Anne
      New blank row here
etc
etc
      etc
Tom
Tom
Total 

Note - No new row between Tom and total


Code:
Sub InsertRowsAtValueChangeColumnB()
  Dim X As Long, LastRow As Long
  Const DataCol As String = "A"
  Const StartRow = 4
  LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
  Application.ScreenUpdating = False
  For X = LastRow To StartRow + 1 Step -1
    If Cells(X, DataCol).Value <> Cells(X - 1, DataCol) Then Rows(X).Insert
  Next
  Application.ScreenUpdating = True
End Sub

This code works fine between row 4 and the last row but I have struggled to get it to work between row 4 and the second to last row, adding this stops a blank row being introduce just before the totals row.

Also, I need to find each of the new empty cells in column B and copy and paste into it the value from the cell below and to the left - column A. For example for new empty cell B10 the value to be copied and pasted into it would be taken from cell A11 so on and so forth.

Any help or pointers would be very much appreciated
 
Starting with data like


Excel 2013/2016
ABC
4NameAreaRevenue
5BobEast£23,000
6BobEast£26,000
7BobEast£24,000
8SallyWest£29,000
9SallyWest£26,000
10SallyWest£32,000
11TaraNorth£15,000
12TaraNorth£22,000
13TaraNorth£28,000
14JimSouth£24,000
15JimSouth£29,000
16JimSouth£36,000
17Total£314,000
Sheet4


And running the code from post#6 I get


Excel 2013/2016
ABC
4NameAreaRevenue
5Bob
6BobEast£23,000
7BobEast£26,000
8BobEast£24,000
9Sally
10SallyWest£29,000
11SallyWest£26,000
12SallyWest£32,000
13Tara
14TaraNorth£15,000
15TaraNorth£22,000
16TaraNorth£28,000
17Jim
18JimSouth£24,000
19JimSouth£29,000
20JimSouth£36,000
21Total£314,000
Sheet4
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Good morning Fluff,

Yes your post shows the start and end of what I am trying to achieve. However the bolded text would only appear in column B and not in column A after it has been copied over.

I did of course copy + paste your code into the VBA editor and run it in my version of Excel - version 2019. It added the blank rows as required but did not copy and paste the text into the relevant cells.

The code is completely unchanged in all respects so why it is not working is baffling. I wouldn't have thought there were any differences between 2013/2016 but perhaps you know the answer to that.

Thanks again for your efforts.
 
Upvote 0
There is no reason why the code should not with 2016.
Are the names in col A hard values or formulae?
Also do you have any merged cells?
 
Upvote 0
Hi Fluff,

I am using Excel 2019 so sorry if you thought otherwise. I had been referring to your code which is annotated above it with 2013/2016 in bold.

The names in column A are entered as plain text and the formatting is set to "General". Text wrapping was on so I removed it and retested but still I get the blank rows with not text copied and pasted into column B.

Finally, no cells within the spreadsheet are merged. I decided on that from day one so it removed the possibility of errors or issues arising from doing such a thing.

Hope this helps.

Thanks
 
Last edited:
Upvote 0
Hi Fluff,

You are a genius!

Your code was working exactly as expected. The reason I didn't see the text was
there was because I had implemented conditional formatting when the spreadsheet
was in its infancy that changed the text colour to white. Doh!

So I'm very sorry you and the other providers of code where wasting your time
on this when in fact you implemented it correctly first time round.

If I may be cheeky can I ask how I can set the copied over text to bold? Then I
will go away and beat myself with a barbed stick.

Many, many thanks for your assistance once more.
 
Last edited:
Upvote 0
To make the font bold, try
Code:
Sub InsertRowsAtValueChangeColumnB()
  Dim X As Long, LastRow As Long
  Const DataCol As String = "A"
  Const StartRow = 4
  LastRow = Cells(Rows.Count, DataCol).End(xlUp).Offset(-1).Row
  Application.ScreenUpdating = False
  For X = LastRow To StartRow + 1 Step -1
    If Cells(X, DataCol).Value <> Cells(X - 1, DataCol) Then
      Rows(X).Insert
      Cells(X, DataCol).Offset(, 1) = Cells(X + 1, DataCol).Value
      [COLOR=#ff0000]Cells(X, DataCol).Offset(, 1).Font.Bold = True[/COLOR]
   End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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