Search code examples
excelvba

How to make picture %60 transparency in vba?


The following video shows how to make picture transparency.

https://www.youtube.com/watch?v=9RpIELN3lyI

I am looking for vba codes which make picture %60 transparency.

Macro recording doesnt work.

First answer of the following link doesnt help because I dont want to add extra rectangle (shape) in order to keep my codes simple.

Second answer of the following link doesnt help because it makes only %100 transparent. I want %60 transparency. It applies to bitmaps only.

Set image transparency with VBA in Excel


Solution

  • Reference the shape and it's Fill.Transparency property.

    enter image description here

    Public Sub Test()
    
        ChangeTransparency "Picture 3", 0.6
        ChangeTransparency "Rectangle 4", 0.3
        ChangeTransparency "Graphic 6", 0.8
    
    End Sub
    
    Public Sub ChangeTransparency(ShapeName As String, TransparencyPercent As Double)
    
        Dim MyShape As Shape
        
        Set MyShape = ThisWorkbook.Worksheets("Sheet1").Shapes(ShapeName)
        MyShape.Fill.Transparency = TransparencyPercent
    
    End Sub  
    

    As a single line:

    ThisWorkbook.Worksheets("Sheet1").Shapes("Rectangle 4").Fill.Transparency = 0.3
    

    Edit:
    Having looked at your second link I think you're after making a picture transparent rather than the background of the picture which you've probably found the code doesn't do if you use insert picture.
    Instead insert a rectangle and set its fill to a Picture or texture fill.
    enter image description here