How to hard code a range of cells to be filled with a background color?

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
I can't believe I that I can't figure this seemingly simple goal of always having a certain range of cells set to a background color. I have tried formatting the range via right clicking the highlighted range and then 'Format cells\fill'. This seems to work, just until I rerun the script, that is when the colored cell ranges change from what I set it up to originally.

Example Goal:

HardCodedRangeFillColorTestV1.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1ColourQty Ordered
2Black14182
3Blue342Red146Green83Blue13Brown410Black25
4Brown4101Blue21Purple27Red42Green61
5Green1963Green52Orange61Orange47
6Orange1082Red05Purple83
7Purple10102Yellow49
8Red18133Black1213
9Yellow491
Sheet1


Wrong result that occurs after rerunning the same script:

HardCodedRangeFillColorTestV1.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1ColourQty Ordered
2Black14182
3Blue342Red146Green83Blue13Brown410Black25
4Brown4101Blue21Purple27Red42Green61
5Green1963Green52Orange61Orange47
6Orange1082Red05Purple83
7Purple10102Yellow49
8Red18133Black1213
9Yellow491
Sheet1


How do I keep the initial range of colored cells from changing? The data displayed hasn't changed, so why are the colored cell ranges changing?
 
Why not just reset the colour at the end of the code
VBA Code:
Range("A:D").Interior.Color = xlNone
Range("A2:D4").Interior.Color = rgbGreen
Indeed that is a better workaround than my loop workaround.

I can't help wonder, why is a workaround needed at all? This is the first time I have ever seen colored cell ranges change on their own.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
They are not changing on their own, you are sorting the cells, which is causing the problem.
 
Upvote 0
They are not changing on their own, you are sorting the cells, which is causing the problem.
Thank you @Fluff.

The solution, that doesn't require a workaround is to use conditional formatting to set the background (fill) color of the cell range. Sorting doesn't 'mess' with the conditional formatting of cells.

Range A2:D4
= AND(ROW() >1,ROW() < 5)
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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