Need: VBA code to add a empty row

steveaust

New Member
Joined
May 25, 2016
Messages
4
I wrote this VBA script to add some colour & text formatting to a pivot table >
The code loops thru column 'C' & examines certain cells in each line of the spreadsheet, adding some formatting as it loops thru to enhance readability (see image below) >

VBA Code:
Private Sub Cell_Color_Change()


  For Each cell In Range("C:C")
        If cell.Value <> "" Then
          Range(cell.Address).Offset(0, 0).Interior.colorIndex = 35
          Range(cell.Address).Offset(0, 1).Interior.colorIndex = 35
            
        ElseIf Range(cell.Address).Offset(0, -1).Value <> "" Then
          Range(cell.Address).Offset(0, -1).Interior.colorIndex = 27
          Range(cell.Address).Offset(0, 0).Interior.colorIndex = 27
          Range(cell.Address).Offset(0, 1).Interior.colorIndex = 27
          Range(cell.Address).Offset(0, 1).Font.Bold = True
    
    
        ElseIf ((Range(cell.Address).Offset(0, -2).Value <> "") And (Range(cell.Address).Offset(0, -1).Value = "") And (Range(cell.Address).Offset(0, 0).Value = "")) Then
          Range(cell.Address).Offset(0, -2).Interior.colorIndex = 46
          Range(cell.Address).Offset(0, -1).Interior.colorIndex = 46
          Range(cell.Address).Offset(0, 0).Interior.colorIndex = 46
          Range(cell.Address).Offset(0, 1).Interior.colorIndex = 46
          Range(cell.Address).Offset(0, -2).Font.Bold = True
          Range(cell.Address).Offset(0, -1).Font.Bold = True
          Range(cell.Address).Offset(0, 0).Font.Bold = True
          Range(cell.Address).Offset(0, 1).Font.Bold = True
          Range(cell.Address).Offset(0, -2).Font.Size = 12
          Range(cell.Address).Offset(0, -1).Font.Size = 12
          Range(cell.Address).Offset(0, 0).Font.Size = 12
          Range(cell.Address).Offset(0, 1).Font.Size = 12
          ' [B]add code here to insert a blank row under the current position in Range()      [/B]

        End If
    
        Next cell
End Sub


1599179028021.png


Question: I want to insert a new empty row under the orange formatted line above, to enhance readability.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlShiftDown
 
Upvote 0
How about this....it saves checking the entire column as well !!

VBA Code:
Private Sub Cell_Color_Change()
Dim cell As Range, lr As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
  For Each cell In Range("C1:C" & lr)
        If cell.Value <> "" Then
          Range(cell.Offset(0, 0), cell.Offset(0, -1)).Interior.ColorIndex = 35
        ElseIf Range(cell.Address).Offset(0, -1).Value <> "" Then
          Range(cell.Offset(0, -1), cell.Offset(0, 1)).Interior.ColorIndex = 27
          Range(cell.Address).Offset(0, 1).Font.Bold = True
        ElseIf ((Range(cell.Address).Offset(0, -2).Value <> "") And (Range(cell.Address).Offset(0, -1).Value = "") And (Range(cell.Address).Offset(0, 0).Value = "")) Then
          With Range(cell.Offset(0, -2), cell.Offset(0, 1))
          .Interior.ColorIndex = 46
          .Font.Bold = True
          .Font.Size = 12
          End With
          cell.Offset(1, 0).EntireRow.Insert
        Range(cell.Offset(1, -2), cell.Offset(1, 1)).Interior.ColorIndex = 0
        End If
        Next cell
End Sub
 
Upvote 0
Little shorter

VBA Code:
Private Sub Cell_Color_Change()
Dim cell As Range, lr As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
  For Each cell In Range("C1:C" & lr)
        If cell.Value <> "" Then
          Range(cell.Offset(0, 0), cell.Offset(0, -1)).Interior.ColorIndex = 35
        ElseIf cell.Offset(0, -1).Value <> "" Then
          Range(cell.Offset(0, -1), cell.Offset(0, 1)).Interior.ColorIndex = 27
          cell.Offset(0, 1).Font.Bold = True
        ElseIf cell.Offset(0, -2).Value <> "" And cell.Offset(0, -1).Value = "" And cell.Offset(0, 0).Value = "" Then
          With Range(cell.Offset(0, -2), cell.Offset(0, 1))
          .Interior.ColorIndex = 46
          .Font.Bold = True
          .Font.Size = 12
          End With
          cell.Offset(1, 0).EntireRow.Insert
        Range(cell.Offset(1, -2), cell.Offset(1, 1)).Interior.ColorIndex = 0
        End If
        Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,527
Members
449,316
Latest member
sravya

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