VBA macro to delete highlighted cells

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
58
Office Version
  1. 2021
Platform
  1. Windows
Hello i need some help again.

I need macro to delete all red highlighted cells. My data goes from BE 1646 to MPT 3252. Cells are highlighted with conditional formatting if this matters...(see first screenshot)

And then i need to remove all blank cells and sort data in same order as before deleting highlighted cells (see second screenshot for example)

Or maybe there is another faster way to do this?

Thanks.
 

Attachments

  • Clipboard01.jpg
    Clipboard01.jpg
    171.5 KB · Views: 21
  • Clipboard03.jpg
    Clipboard03.jpg
    140.6 KB · Views: 21

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.
Try on a copy.
VBA Code:
Sub DeleteRedCellsAndShiftUp2()
    Dim rng As Range
    Dim cell As Range
    Dim redCells As Range
    Dim redCount As Long
 
    Set rng = Range("BE1646:MPT3252")
    For Each cell In rng
        If cell.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
            If redCells Is Nothing Then
                Set redCells = cell
            Else
                Set redCells = Union(redCells, cell)
            End If

        End If
    Next cell
 
    If Not redCells Is Nothing Then
        redCells.ClearContents
    End If

    rng.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End Sub
 
Last edited:
Upvote 1
Try on a copy.
VBA Code:
Sub DeleteRedCellsAndShiftUp2()
    Dim rng As Range
    Dim cell As Range
    Dim redCells As Range
    Dim redCount As Long
 
    Set rng = Range("BE1646:MPT3252")
    For Each cell In rng
        If cell.DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
            If redCells Is Nothing Then
                Set redCells = cell
            Else
                Set redCells = Union(redCells, cell)
            End If

        End If
    Next cell
 
    If Not redCells Is Nothing Then
        redCells.ClearContents
    End If

    rng.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End Sub
Thanks. This works but it takes very very long time to process even if i limit range to just 20 columns... Anyone have any other idea how to do this faster?
 
Upvote 0
Maybe this
VBA Code:
Sub DeleteRedCellsAndShiftUp3()
    Dim rng As Range
    Dim cell As Range
    Dim redCells As Range
    Dim redCount As Long
    Dim dataArray As Variant
    Dim i As Long, j As Long

    Set rng = Range("BE1646:MPT3252")

    dataArray = rng.Value
    For i = 1 To UBound(dataArray, 1)
        For j = 1 To UBound(dataArray, 2)
            If dataArray(i, j) <> "" Then 
                If rng.Cells(i, j).DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                    If redCells Is Nothing Then
                        Set redCells = rng.Cells(i, j)
                    Else
                        Set redCells = Union(redCells, rng.Cells(i, j))
                    End If
                End If
            End If
        Next j
    Next i

    If Not redCells Is Nothing Then
        redCells.ClearContents
    End If

    rng.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End Sub
 
Upvote 0
This took just under 1 second for 20 columns, although it took 6.5 minutes for your full 14M+ cells - I'm working on making that faster. Please try on a copy of your workbook.

VBA Code:
Option Explicit
Sub Delete_Red_Cells()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change sheet name to suit ***
    Dim r As Range
    Set r = ws.Range("BE1646").CurrentRegion
    
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 0, 0) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("BE1646").Resize(LRow, LCol).Value = b
    MsgBox Timer - t
End Sub
 
Upvote 1
Solution
Maybe this
VBA Code:
Sub DeleteRedCellsAndShiftUp3()
    Dim rng As Range
    Dim cell As Range
    Dim redCells As Range
    Dim redCount As Long
    Dim dataArray As Variant
    Dim i As Long, j As Long

    Set rng = Range("BE1646:MPT3252")

    dataArray = rng.Value
    For i = 1 To UBound(dataArray, 1)
        For j = 1 To UBound(dataArray, 2)
            If dataArray(i, j) <> "" Then
                If rng.Cells(i, j).DisplayFormat.Interior.Color = RGB(255, 0, 0) Then
                    If redCells Is Nothing Then
                        Set redCells = rng.Cells(i, j)
                    Else
                        Set redCells = Union(redCells, rng.Cells(i, j))
                    End If
                End If
            End If
        Next j
    Next i

    If Not redCells Is Nothing Then
        redCells.ClearContents
    End If

    rng.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End Sub
This is faster then previous one at deleting cells but still slow at sorting...
This took just under 1 second for 20 columns, although it took 6.5 minutes for your full 14M+ cells - I'm working on making that faster. Please try on a copy of your workbook.

VBA Code:
Option Explicit
Sub Delete_Red_Cells()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change sheet name to suit ***
    Dim r As Range
    Set r = ws.Range("BE1646").CurrentRegion
   
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 0, 0) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("BE1646").Resize(LRow, LCol).Value = b
    MsgBox Timer - t
End Sub
This one is lighting fast! Im happy with this. Waiting couple of minutes for so much data its no problem. Thank you very much.

One more question... What i need to do if i want to delete white cells(non highlighted) so just opposite. Im guessing i just need to change RGB(255, 0, 0)? To what? Can you post example please.
 
Upvote 0
Happy to help & thanks for the feedback (y) :)

'White' cells should be RGB(255,255,255) therefore:
VBA Code:
If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 255, 255) Then
 
Upvote 1
Or if there's no interior color set, you could try:
VBA Code:
If r.Cells(i, j).DisplayFormat.Interior.ColorIndex = 0 Then
 
Upvote 1
This took just under 1 second for 20 columns, although it took 6.5 minutes for your full 14M+ cells - I'm working on making that faster. Please try on a copy of your workbook.

VBA Code:
Option Explicit
Sub Delete_Red_Cells()
    Dim t As Double: t = Timer
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<-- *** Change sheet name to suit ***
    Dim r As Range
    Set r = ws.Range("BE1646").CurrentRegion
   
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    Dim i As Long, j As Long, k As Long, LRow As Long, LCol As Long
    LRow = r.Rows.Count
    LCol = r.Columns.Count
    k = 1
    For j = 1 To LCol
        For i = 1 To LRow
            If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 0, 0) Then
                b(k, j) = a(i, j)
                k = k + 1
            End If
        Next i
        k = 1
    Next j
    ws.Range("BE1646").Resize(LRow, LCol).Value = b
    MsgBox Timer - t
End Sub
I ended up using this VBA quite often and for 14M cells still takes quite a while to process...
Can you look in to this and make it even faster if possible?

My sheets are packed with conditional formatting formulas similar to this "(=AND($BC2=3;BE2>=-4;BE2<=65)" if this matters... This probably affects the speed too...

Thanks.
 
Upvote 0
14M plus cells is still 14M plus cells so expect some time lag😉
Conditional formatting will definitely slow things down - is it necessary; are there other ways (VBA) of achieving the same formatting after all other processing has occurred; or is it the CF that gives the cells their red colour? In which case, is there another way we can use the values in the cells (rather than the colour) to perform the delete?
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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