macro to clear rows that are conditionally filled with a color

vettepwr

New Member
Joined
Jun 17, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am trying to convert a sheet I made on Google Sheets over to Excel to share with some co-workers. Obviously my google apps code doesnt work in excel and I not familiar with vba that much.

I need to be able to create a macro that I can assign to a button that will clear the contents of rows that are conditionally formatted with a filled color.

This is the code I was using in google, each function being linked to 3 separate buttons.

function CLEARAUTH() {
var sheet = SpreadsheetApp.getActive().getSheetByName('CLAIMS TRACKER');
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
for (var i=0; i<bgColors.length; i++) {
for (var j=0; j<bgColors.length; j++) {
if (bgColors[j] === '#6aa84f') {
range.getCell(i+1,j+1).clearContent();
}
}
}
}
function CLEARDENIED() {
var sheet = SpreadsheetApp.getActive().getSheetByName('CLAIMS TRACKER');
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
for (var i=0; i<bgColors.length; i++) {
for (var j=0; j<bgColors.length; j++) {
if (bgColors[j] === '#b7b7b7') {
range.getCell(i+1,j+1).clearContent();
}
}
}
}
function CLEARINACTIVE() {
var sheet = SpreadsheetApp.getActive().getSheetByName('CLAIMS TRACKER');
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
for (var i=0; i<bgColors.length; i++) {
for (var j=0; j<bgColors.length; j++) {
if (bgColors[j] === '#0000ff') {
range.getCell(i+1,j+1).clearContent();
}
}
}
}

What I need is a VBA version to do the same thing.
I need the macro to look at rows from cells B3 - T100 check for the color assigned by the conditional formatting and then clear the contents of the rows but only the data in Columns B-T Rows 3 - 100
I have been trying to figure this out for hours now and just cant seem to get it working. Any help would be greatly appreciated.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi & welcome to MrExcel.
Will the entire row be filled with the colour, or just some cells?
If just some cells, should only those be cleared or the entire row?
 
Upvote 0
Hi & welcome to MrExcel.
Will the entire row be filled with the colour, or just some cells?
If just some cells, should only those be cleared or the entire row?
The conditional formatting only applies a fill color to columns B-T and only the data in the cells from B-T needs cleared.
 
Upvote 0
Yes but will all cells from B to T on a particular row be filled, or only some?
 
Upvote 0
Ok, how about
VBA Code:
Sub vettepwr()
   Dim Cl As Range
   
   For Each Cl In Range("B3:B100")
      If Cl.DisplayFormat.Interior.Color = 5220458 Then Cl.Resize(, 19).ClearContents
   Next Cl
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub vettepwr()
   Dim Cl As Range
  
   For Each Cl In Range("B3:B100")
      If Cl.DisplayFormat.Interior.Color = 5220458 Then Cl.Resize(, 19).ClearContents
   Next Cl
End Sub
I am going to try that here shortly. Will post back with results. Thank you so much
 
Upvote 0
Awesome that worked perfectly. Now I see where I can change the color its searching for but I am not sure how you converted my hex color code to the number you used in the code. I am going to go see if I can figure that part out on my own, unless you happen to reply back before I do.

Ill just need to edit that color code to make the other two macros.
 
Upvote 0
Yep got it. useda hex to rgb converter and used RGB(xx, xx, xx) format

Thanks so much again. Im going to go work on finishing this sheet to excel conversion.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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