VBA adding a blank row in column D when values change, only in visible cells - Excel

Madter

New Member
Joined
Oct 16, 2017
Messages
24
Hi!
I want my VBA code to insert two blank rows whenever the cell value in column d changes. And since i have used some autofilters, i only want it to do this function on visible cells.

I have this:
Code:
[/FONT][/COLOR][COLOR=#242729][FONT=Arial][COLOR=#222222][FONT=Verdana]Dim GCell As Range[/FONT][/COLOR][/FONT][/COLOR]
SearchText = ""
Set GCell = Cells.Find(SearchText).Offset(0)
GCell.EntireRow.Insert
[COLOR=#242729][FONT=Arial][COLOR=#222222][FONT=Verdana]GCell.EntireRow.Insert[/FONT][/COLOR][/FONT][/COLOR]


But it only works if you put in a certain text you want to search for. But here i don't have anything specific to search for. The values in the column are just "random" numbers.
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Madter,

This is a convoluted way to do this, but I think it should work. I would create a test copy of your data before stepping through it.

Code:
Sub Add_Blank_Cells()

    LR = Range("D" & Rows.Count).End(xlUp).Row
    previousCell = ""

    For Each currentCell In Range("D2:D" & LR).SpecialCells(xlCellTypeVisible)
    
        If currentCell <> "" Then
        
            If currentCell <> previousCell And previousCell <> "" Then
                Rows(currentCell.Row).Insert
                Rows(currentCell.Row).Insert
            End If
        
        End If
        
        previousCell = currentCell.Value
    Next currentCell
  
End Sub

Let me know if you have any questions or if it worked for you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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