Remove data which isn't highlighted and shift it all to the left.

chanman

New Member
Joined
Oct 14, 2008
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi - Some help required please!

Anyone able to help me with the follow?
Book1
ABCDE
1Sample1Sample2Sample3Sample4Sample5
23A2BG
313211
41A3B3
514231
633213
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:E6Cell Value=3textNO


Required output;
Book1
ABCDE
1Sample1Sample2Sample3Sample4Sample5
23
33
433
53
6333
Sheet2


thanks in advance!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
If I'm not mistaken (and I often am) you may have to use code because of the cell shifting. AFAIK, the only reliable way is to loop over the range backwards (because it is changing as you delete cells). In your case, if the cell <> 3 delete it, shift left and continue. If you have tens of thousands of rows and many columns, that will likely be be quite slow. How large is the range?
 
Upvote 0
Signing off here for the night. In case your range is not too large, I came up with the following (works with your sample data):
VBA Code:
Sub KeepOnly3()
Dim Lcol As Long, Lrow As Long, i As Long
Dim sht As Worksheet

On Error GoTo errHandler
Application.EnableEvents = False
Set sht = Sheets("008")
'find the last row and last column with data
Lrow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Lcol = sht.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Do Until Lrow = 1
    For i = Lcol To 1 Step -1
        If Cells(Lrow, i) <> "" And Not CStr(Cells(Lrow, i)) = "3" Then Cells(Lrow, i).Delete Shift:=xlToLeft
    Next i
    Lrow = Lrow - 1
Loop

exitHere:
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
End Sub
Put the code in a standard module and change the sheet name in the code to your sheet name.
Test on a backup copy of your workbook.
 
Upvote 0
Couple of alternatives offered. The first one should do if there aren't too many rows. The second one is better if there are thousands of rows. Just change the sheet name to suit.
VBA Code:
Option Explicit
Sub chanman_V1()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")       '<-- *** Change to actual sheet name ***
    Dim r As Range, c As Range
    For Each c In ws.Range("A2", ws.Cells(Rows.Count, "E").End(xlUp))
        If c <> "3" Then
            If r Is Nothing Then Set r = c Else Set r = Union(r, c)
        End If
    Next c
    r.Delete shift:=xlToLeft
    Application.ScreenUpdating = True
End Sub



VBA Code:
Sub chanman_V2()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")       '<-- *** Change to actual sheet name ***
    Dim a, b, i As Long, j As Long, k As Long
    ws.Columns("A:E").Interior.Color = xlNone
    a = ws.Range("A2", ws.Cells(Rows.Count, "E").End(xlUp))
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    k = 1
    
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(a, 2)
            If a(i, j) = "3" Then
                b(i, k) = "3"
                k = k + 1
            End If
        Next j
        k = 1
    Next i
    
    With ws
        .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        With .UsedRange
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlTextString, String:="3", TextOperator:=xlContains
            .FormatConditions(1).Interior.Color = vbYellow
        End With
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming the values are not the result of formulas & that there will be at least one cell to delete ..

A. If we can use the "3" value to determine what to keep (as the earlier suggestions have done, then you could try this

VBA Code:
Sub Move_Left_v1()
  With Range("A2", Range("E" & Rows.Count).End(xlUp))
    .Value = Evaluate("if(" & .Address & "=3,3,True)")
    .SpecialCells(xlConstants, xlLogical).Delete Shift:=xlToLeft
  End With
End Sub

B. If the requirement is to use the cell's colour not being yellow (as indicated in the thread title) like this sample data where the conditional formatting highlights odd numbers and the letter "G" then try the code below.

chanman.xlsm
ABCDE
1Sample1Sample2Sample3Sample4Sample5
23A2BG
313211
41A5B3
514231
633213
Sheet3
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:E6Expression=IFERROR(ISODD(A2),A2="G")textNO


VBA Code:
Sub Move_Left_v2()
  Dim a As Variant
  Dim r As Long, c As Long
 
  With Range("A2", Range("E" & Rows.Count).End(xlUp))
    a = .Value
    For r = 1 To UBound(a)
      For c = 1 To UBound(a, 2)
        If .Cells(r, c).DisplayFormat.Interior.Color <> vbYellow Then a(r, c) = True
      Next c
    Next r
    .Value = a
    .SpecialCells(xlConstants, xlLogical).Delete Shift:=xlToLeft
  End With
End Sub

This is the result of the v2 code on the sample sheet above

chanman.xlsm
ABCDE
1Sample1Sample2Sample3Sample4Sample5
23G
31311
4153
5131
63313
Sheet3
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:D3,A6:D6,A4:C5,A2:B2Expression=IFERROR(ISODD(A2),A2="G")textNO
 
Upvote 0
Solution
Does this non-looping macro do what you want...
VBA Code:
Sub KeepHighlightedOnly()
  With Application.FindFormat
    .Clear
    .Interior.ColorIndex = 0
    With Range("A2", Range("E" & Rows.Count).End(xlUp))
      .Replace "", "", , , , , True, False
      .SpecialCells(xlBlanks).Delete xlShiftToLeft
    End With
    .Clear
  End With
End Sub
 
Last edited:
Upvote 0
Thanks all for the responses!
I think my sample data may have mislead some people. Apologies! ( @Micron @kevin9999 ) I over simplified the highlighted values. I required the highlighted cells to be kept regardless of the cell content.

@Peter_SSs v2 worked exactly as required!

@Rick Rothstein - fyi your code left me with an empty sheet

Thank again all, what a great forum and lovely bunch you all are!
 
Upvote 0
Thanks for the feedback chanman :) (y)
Although you have a solution, I always feel compelled to correct any previously shared code now new conditions are known. So just for my own benefit:
VBA Code:
Option Explicit
Sub chanman_V3()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")       '<-- *** Change to actual sheet name ***
    Dim r As Range, c As Range
    For Each c In ws.Range("A2", ws.Cells(Rows.Count, "E").End(xlUp))
        If c.DisplayFormat.Interior.Color <> vbYellow Then
            If r Is Nothing Then Set r = c Else Set r = Union(r, c)
        End If
    Next c
    r.Delete shift:=xlToLeft
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I required the highlighted cells to be kept regardless of the cell content.
I don't understand how that can be when in the first post your cf condition is cell equals 3. Oh well.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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