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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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