Keep only data that is highlighted

Serafin54

Board Regular
Joined
Apr 11, 2014
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Is there a way via VBA to select a range of columns in a worksheet and then clear out any cell that isn't highlighted so I'm not going column by column to filter out cells with no fill and doing it manually? The columns that are needed to be cleared out may differ time and again so i can't say that the range will always be (B:M).

I was trying a couple codes i found online and tweak them but I'm heading down a rabbit hole quickly and record macro is really bulky and not forgiving.

  • On this example for example, I want to be able to select the columns i want to run the code or the ones not to include. either or but thinking selecting which to include is easiest for coding. it will not always just be omitting column A. It could be A-D being omitted or etc.
  • Have the code ignore the header row
  • Clear the cells of any that are not highlighted in any way.
I am able to accomplish it via find/replace but just having a macro power tool at the ready to do all the steps needed would save time.

Thanks!!!!!

before:

Book1
ABCDEFGHIJKLM
1DateEast ColorEast UnitsEast SalesNorth ColorNorth UnitsNorth SalesSouth ColorSouth UnitsSouth SalesWest ColorWest UnitsWest Sales
201/03/2016xxxxxxxxx1111
301/13/2016xxxxxx1896xxx
401/21/2016xxxxxxxxx1226
501/30/2016xxx1784xxxxxx
602/07/2016xxx18104xxxxxx
702/13/2016xxxxxx1222xxx
802/21/20161560xxxxxxxxx
903/01/2016xxxxxxxxx1226
1003/13/20161896xxxxxxxxx
1103/23/2016xxx1784xxxxxx
1203/28/2016xxxxxxxxx1226
1304/03/2016xxxxxx1896xxx
1404/12/2016xxxxxx1113xxx
1504/16/20161888xxxxxxxxx
1604/23/2016xxxxxxxxx1666
1704/30/2016xxxxxx1565xxx
1805/09/2016xxxxxx1784xxx
1905/16/2016xxxxxx1555xxx
2005/25/2016xxxxxx1112xxx
2105/30/2016xxx1448xxxxxx
2206/04/2016xxx1784xxxxxx
2306/13/20161333xxxxxxxxx
2406/21/2016xxxxxx1224xxx
2506/26/2016xxxxxx1672xxx
2607/02/20161666xxxxxxxxx
2707/08/2016xxxxxxxxx1226
2807/12/2016xxxxxx1448xxx
2907/19/2016xxxxxx1113xxx
3007/26/2016xxxxxxxxx1112
3107/31/2016xxxxxx1222xxx
3208/07/2016xxxxxxxxx1565
3308/13/2016xxxxxxxxx1560
3408/20/2016xxx18104xxxxxx
3508/25/2016xxxxxxxxx1448
3609/01/2016xxxxxx1666xxx
3709/07/2016xxxxxx1791xxx
3809/10/2016xxx1444xxxxxx
Original


After:

Book1
ABCDEFGHIJKLM
1DateEast ColorEast UnitsEast SalesNorth ColorNorth UnitsNorth SalesSouth ColorSouth UnitsSouth SalesWest ColorWest UnitsWest Sales
201/03/20161111
301/13/20161
401/21/20161
501/30/20161
602/07/20161104
702/13/20161
802/21/20161
903/01/20161
1003/13/20161
1103/23/20161
1203/28/20161
1304/03/20161
1404/12/20161113
1504/16/20161
1604/23/20161
1704/30/20161
1805/09/20161
1905/16/20161
2005/25/20161112
2105/30/20161
2206/04/20161
2306/13/20161
2406/21/20161
2506/26/20161
2607/02/20161
2707/08/20161
2807/12/20161
2907/19/20161113
3007/26/20161112
3107/31/20161
3208/07/20161
3308/13/20161
3408/20/20161104
3508/25/20161
3609/01/20161
3709/07/2016191
3809/10/20161
Results
 
no. that is fine. that's a great addition to stop at the last line. Works perfectly. Thank you!
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Just noting that you can do what you want without using a loop...
VBA Code:
Sub ClearUnhighlightedCells()
  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    .FindFormat.Interior.ColorIndex = 0
    ActiveSheet.UsedRange.Replace "", "", , , , , True, False
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub
 
Upvote 0
Just noting that you can do what you want without using a loop...
VBA Code:
Sub ClearUnhighlightedCells()
  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    .FindFormat.Interior.ColorIndex = 0
    ActiveSheet.UsedRange.Replace "", "", , , , , True, False
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub
rick could you clarify your code. The user wanted a code that applied to a manual selection. Would yours not clear the entire 'Used Range' of the sheet rather than a user defined selection.
 
Upvote 0
rick could you clarify your code. The user wanted a code that applied to a manual selection.
Whoops! I missed that... thanks for catching it. The change is simple though, just use Selection in place of the ActiveSheet.UsedRange...
VBA Code:
Sub ClearUnhighlightedCells()
  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    .FindFormat.Interior.ColorIndex = 0
    Selection.Replace "", "", , , , , True, False
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub
 
Upvote 0
Whoops! I missed that... thanks for catching it. The change is simple though, just use Selection in place of the ActiveSheet.UsedRange...
VBA Code:
Sub ClearUnhighlightedCells()
  With Application
    .ScreenUpdating = False
    .FindFormat.Clear
    .FindFormat.Interior.ColorIndex = 0
    Selection.Replace "", "", , , , , True, False
    .FindFormat.Clear
    .ScreenUpdating = True
  End With
End Sub
Thanks Rick. I did try it but with the other code, the header row was omitted from being cleared out so that I would still be able to see the classification type. Other than that, it does work well
 
Upvote 0
I am not entirely clear on whether there is still an issue with my latest code or not. Did it work correctly for you are is there something (related to headers) that it still needs to address?
 
Upvote 0
I am not entirely clear on whether there is still an issue with my latest code or not. Did it work correctly for you are is there something (related to headers) that it still needs to address?
Hi. Sorry. So what is needed of the code is that even though the user is selecting the entire column, the header doesn't get cleared out. Only the items beneath them.
In your code, if i select the entire column, since the header isn't highlighted, it gets cleared out which leaves no indicator (header). That would be the only update I can see.
 
Upvote 0
Hi. Sorry. So what is needed of the code is that even though the user is selecting the entire column, the header doesn't get cleared out. Only the items beneath them.
In your code, if i select the entire column, since the header isn't highlighted, it gets cleared out which leaves no indicator (header). That would be the only update I can see.
The code I supplied does exactly what you asked for. It leaves the header in place
 
Upvote 0
The code I supplied does exactly what you asked for. It leaves the header in place

I am aware and I marked yours as the solution. However i can't ignore someone else's solution and feel only fair to give feedback. I have already used yours and it does what is needed. Thanks again.
 
Upvote 0
Thanks Rick. I did try it but with the other code, the header row was omitted from being cleared out so that I would still be able to see the classification type. Other than that, it does work well

Hope, you don't select the headers.
For me Rick's VBA cod works very well.
 
Upvote 0

Forum statistics

Threads
1,215,899
Messages
6,127,637
Members
449,393
Latest member
Messi1408

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