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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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