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
I give cells color in this way as member "awoohaw" suggested in bottom link in post 2. And then i sort data with VBA you gave me.
Is this possible to do with VBA or any other way to speed things up? It all works well just takes a while... Im a bit rusty in excel and not as good as others heh :)

Link:
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The conditional formatting seems unnecessary if you're just looking to delete cells with values between 4 and 65 inclusive.
 
Upvote 0
Remove the conditional formatting and try change
VBA Code:
If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 0, 0) Then
to
VBA Code:
If a(i, j) >= -4 And a(i, j) <= 65 Then

Test on a copy.
 
Upvote 0
Remove the conditional formatting and try change
VBA Code:
If r.Cells(i, j).DisplayFormat.Interior.Color <> RGB(255, 0, 0) Then
to
VBA Code:
If a(i, j) >= -4 And a(i, j) <= 65 Then

Test on a copy.
This works instant just 25 seconds nice. But it deletes values across whole range. What i need is to keep values between -4 and 65 just in rows where value 3 is in column C.
So in this case just row 6,10 and 15 (see screenshot)

Is this possible?
 

Attachments

  • 1.png
    1.png
    36.3 KB · Views: 5
Upvote 0
The objective of this request isn't the same as the original post. Post a new thread.
 
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
Hey guys. Its me again heh :cool:

Can you help me modify this VBA so the data is sorted on bottom? So the same as question in first post just data sorted on bottom (see screenshot).
 

Attachments

  • 1.png
    1.png
    44.5 KB · Views: 8
Upvote 0
You really should start a new thread for this, however, try adding the following code after the line
VBA Code:
ws.Range("BE1646").Resize(LRow, LCol).Value = b

New code:
VBA Code:
Dim rng As Range
    Set rng = Range("BE1646").CurrentRegion
    LRow = rng.Rows.Count
    LCol = rng.Columns.Count
    k = 0
    For j = 1 To LCol
        For i = LRow To 1 Step -1
            If rng.Cells(i, j) = "" Then k = k + 1
        Next i
        If k > 0 Then Range(rng.Cells(1, j), rng.Cells(k, j)).Insert Shift:=xlDown
        k = 0
    Next j
 
Upvote 1

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
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