Copy and paste into blank cells only

kris_91

New Member
Joined
May 5, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Hi all, I really hope you can help me. I have an issue writing my code. I have a range, where first I am inserting blank cells whenever the value/text in column N changes - this I have solved. Now I want to copy the header (row 1) into all the newly created blank rows. However this should only be done in a dynamic range (maybe using a formula to find last used row). Really hoping you can help.
Sub InsertRowsAtValueChangeColumn()
Dim X As Long, LastRow As Long
Const DataCol As String = "N"
Const StartRow = 2
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
'rows(1).EntireRow.copy
'Selection.SpecialCells(xlCellTypeBlanks.PasteSpecial



'Range("A1:X1").Select
'Selection.copy
'BlankCell.Paste
'Destination = Selection.SpecialCells(xlCellTypeBlanks)


End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,135
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub InsertRowsAtValueChangeColumn()
Dim X As Long, LastRow As Long
Dim Hdr As Variant

Const DataCol As String = "N"
Const StartRow = 2
LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
Application.ScreenUpdating = False
Hdr = Range("A1:X1").Value2
For X = LastRow To StartRow + 1 Step -1
If Cells(X, DataCol).Value <> Cells(X - 1, DataCol) Then
   Rows(X).Insert
   Range("A" & X).Resize(, 24).Value = Hdr
End If
Next

End Sub
 
Solution

kris_91

New Member
Joined
May 5, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
wow it worked like a charm! Thank you so much. If I also want to copy the font of the header (row1) do you know how to do that??
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,135
Office Version
  1. 365
Platform
  1. Windows
Do all the header cells have the same font? If so what is it?
 

kris_91

New Member
Joined
May 5, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

They all have the same colour
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0
.PatternTintAndShade = 0
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,135
Office Version
  1. 365
Platform
  1. Windows
Is that the font colour or the interior colour?
 

kris_91

New Member
Joined
May 5, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
That is the interior colour! Font colour is just black
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,135
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
If Cells(X, DataCol).Value <> Cells(X - 1, DataCol) Then
   Rows(X).Insert
   With Range("A" & X).Resize(, 24)
      .Value = Hdr
      .Interior.ThemeColor = xlThemeColorAccent3
   End With
End If
Next
 

Watch MrExcel Video

Forum statistics

Threads
1,132,642
Messages
5,654,547
Members
418,139
Latest member
nimesh72

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