Highlight Cells Using VBA

anwilson

New Member
Joined
Apr 17, 2019
Messages
13
Mornin all.

I have a code that selects certain cells based on the user's input and highlights them yellow or gray. If possible I'd like to be able to set the cells I want highlighted equal to a range of some sort then highlight the range so the sheet isn't jumping everywhere (by selecting them) when it highlights the cells. See below for example:

sh2.Rang("B13:31").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColoIndex = xlAutomatic
.ThemeColor = xlThemeColorDarkl
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With

Thanks in advance!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try
Code:
With sh2.Range("B13:B31")
   .Pattern = xlSolid
   .PatternColoIndex = xlAutomatic
   .ThemeColor = xlThemeColorDarkl
   .TintAndShade = -0.149998474074526
   .PatternTintAndShade = 0
End With
 
Upvote 0
You do not need to select the cells to change the color.

Code:
With sh2.Range("B13:B31").Interior
    .Pattern = xlSolid
    .PatternColoIndex = xlAutomatic
    .ThemeColor = xlThemeColorDarkl
    .TintAndShade = -0.149998474074526
    .PatternTintAndShade = 0
End With
 
Upvote 0
It's giving me the error:

"Object doesn't support this property or method"

on the line defining the pattern. Any idea why?
 
Upvote 0
How about
Code:
With sh2.Range("B13:B31").Interior
   .Pattern = xlSolid
   .PatternColorIndex = xlAutomatic
   .ThemeColor = xlThemeColorDarkl
   .TintAndShade = -0.149998474074526
   .PatternTintAndShade = 0
End With
 
Upvote 0
That helped with the Pattern error, now it's giving me an error of:

"Subscript out of range"

on the ThemeColor line.

Thanks for your continued help, you are much appreciated!
 
Upvote 0
try changing it to
Code:
.ThemeColor = xlThemeColorDark1
 
Upvote 0
Try
Code:
.ThemeColor = xlThemeColorDark1
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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