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.
 
Try this after adjusting the values for oneShape and oneCell.

Code:
Sub test()
    Dim oneShape As Shape
    Dim oneCell As Range
    Set oneShape = ActiveSheet.Shapes(1)
    Set oneCell = Range("Q20")
    
    With oneShape
        .Fill.ForeColor.RGB = oneCell.DisplayFormat.Interior.Color
        .Top = oneCell.Top
        .Left = oneCell.Left
        .Height = oneCell.Height
        .Width = oneCell.Width
        With .TextFrame
            With .Characters
                With .Font
                    .Color = oneCell.Font.Color
                    .Name = oneCell.Font.Name
                    .Size = oneCell.Font.Size
                End With
                .Text = "cat"
            End With
        End With
        With .TextFrame2
            .VerticalAnchor = msoAnchorMiddle
        End With
    End With
End Sub
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Thank you! It indeed looks like the same rectangle cannot be created manually to be transparent.The problem is that in addition to numbers, the underlying cells also have colours from conditional formatting. I can use your suggestion plus loop through all the cells, copying their values to rectangles. It looks like I would also need to recreate the conditional formatting in VBA.

The first time I encountered a formatting done by Excel which cannot be replicated manually...

EDIT: actually your code also preserves the colour from the conditional formatting which is great!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
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