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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
 
Upvote 0
Solution
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??
 
Upvote 0
Do all the header cells have the same font? If so what is it?
 
Upvote 0
They all have the same colour
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0
.PatternTintAndShade = 0
 
Upvote 0
Is that the font colour or the interior colour?
 
Upvote 0
That is the interior colour! Font colour is just black
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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