Adding a shadow for a cell in VBA (macro recording fails)

peten

New Member
Joined
Jan 24, 2019
Messages
7
I try to add a shadow in a cell (Format toolbar - Shape effects - shadow). However, if I try to do the same in macro recording, the recording is blank. When one clicks the Shape effects - shado, Excel actually creates a rectangle just the size of the cell and gives it a shadow; somehow the recording fails to capture this.

If I create a random shape and run the macro, the relevant lines will be:
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.Shadow.Type = msoShadow22

But how can I make a VBA to apply these to cells (not to a shape)? I'd like to loop through a range and repeat this for each cell.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this:
Right click cell
Choose Format Cell
Choose Fill
Choose Fill Effect

See if that does what you want.

If you want Vba code to do this:
Use the Macro recorder
 
Last edited:
Upvote 0
If you want a macro to do the same formatting in a range of cells.

Here is what I did.

Now this color may not be exactly what you want.

So you may want to use the Macro Recorder on one cell. Or if the range will always be the same

Select the range and then do the steps I said earlier

My script here using the selection which is what ever selection you choose.

I ran the recorder modified the script and just used selection not specifying what selection.

So now what ever selection I select will be formatted the way the script says.
Code:
Sub Color_Me()
'Modified  1/24/2019  4:31:51 AM  EST
    
    With Selection.Interior
        .Pattern = xlPatternLinearGradient
        .Gradient.Degree = 270
        .Gradient.ColorStops.Clear
    End With
    With Selection.Interior.Gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.Interior.Gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0
    End With
End Sub
 
Upvote 0
Try this:
Right click cell
Choose Format Cell
Choose Fill
Choose Fill Effect

See if that does what you want./QUOTE]

This creates a gradient fill, not a shadow. Anyway, macro recording yields:

With Selection.Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 90
.Gradient.ColorStops.Clear
End With

Tried to apply this to the shadow, but this did not work:

With Selection.Interior 'also tried without .Interior
.Shadow.Type = msoShadow22
End With
 
Upvote 0
Actually, if the cell has conditional formatting, the gradient fill will not work while shadow still produces the effect (since it is not in the cell itself but in a shape placed over the cell).
 
Upvote 0
I just gave a suggestion.

You can play with the recorder.

Cells probable cannot be formatted the same as shapes.
But maybe someone else here on Mr. Excel will have another answer.
 
Upvote 0
Since there was no luck with the recorded, I tried to do this the hard way, i.e. creating a rectangle over each cell using VBA. Another strange difficulty arose: it does not seem to be possible to create a similar rectangle manually as the one added by excel. Please see the comparison:



bLFa7Te
The rectangle around 0.64 you can add e.g. typing "shadow" in the "Tell me what you want to do" box and select the first outer shadow under Shape Effects. Then go to format menu and change Shape outline to none.

But try to do the same yourself by adding a rectangle, then in format menu add shadow, remove shape fill and outline, i.e. the same settings you can see in the excel generated rectangle. I left the outline in the rectangle around 0.82, if you remove it, your rectangle becomes just a transparent box. Even with the outline it does not look the same.

Please let me know if I'm missing something obvious...
 
Upvote 0
If you make the interior fill of the rectangle white, rather than no-fill, and then remove the border, the image should be like the other
 
Upvote 0
If you make the interior fill of the rectangle white, rather than no-fill, and then remove the border, the image should be like the other

If you do that, the image looks the same but you can no longer see the number underneath (white rectangle is is no longer transparent).
 
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