excel 通过点击形状改变形状的背景图像

qv7cva1a  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(116)

我正在寻找一个图形,将通过背景图像(保存在我的电脑上)通过点击形状内显示循环的代码。
我发现两套代码,给予我一个良好的开端,但我不知道如何合并代码,以获得我正在寻找的结果。
我希望每次点击形状保持循环通过以下命令:
1.初始形状:透明背景
1.单击形状:透明背景替换为BackgroundImage1
1.另一个单击形状:BackgroundImage1替换为BackgroundImage2
1.另一个单击形状:BackgroundImage2替换为透明背景
我发现这段代码可以通过点击来改变形状的颜色:

Sub trafficlight()
    Dim WhoAmI As String, sh As Shape
    WhoAmI = Application.Caller
    With ActiveSheet.Shapes(WhoAmI).Fill.ForeColor
        Select Case .RGB
            Case vbRed
                .RGB = vbGreen
            Case vbGreen
                .RGB = vbYellow
            Case Else
                .RGB = vbRed
        End Select
    End With
End Sub

这段代码来改变形状与图像保存在我的电脑上:

Sub Rectangle9_Click()
    Dim WhoAmI As String, sh As Shape
    WhoAmI = Application.Caller
    With ActiveSheet.Shapes(WhoAmI).Fill
        .Visible = msoTrue
        .UserPicture "C:\Users\username\Desktop\BackgroundImage1.png"
        .TextureTile = msoFalse
    End With
End Sub
epfja78i

epfja78i1#

您需要跟踪当前显示的图像。您可以为每次图像更改设置integer

Option Explicit

Sub ChangeShapePic()
Static i As Integer

With ActiveSheet.Shapes(Application.Caller).Fill
    Select Case i
        Case 0
            .UserPicture ("C:\Users\username\Desktop\BackgroundImage1.png")
            i = 1
        Case 1
            .UserPicture ("C:\Users\username\Desktop\BackgroundImage2.png")
            i = 2
        Case 2
            .UserPicture ("C:\Users\username\Desktop\BackgroundImage3.png")
            i = 3
        Case 3
           .Solid
           .Transparency = 0#
            i = 0
    End Select
End With
End Sub
dfddblmv

dfddblmv2#

你可以使用代码:删除背景图像/图片作为使用工具图片格式->在Excel中设置透明颜色。

Sub RemoveBackground()
   Dim selectedPicture As Picture
    Set selectedPicture = ActiveSheet.Pictures("Picture 3")
 ' Set the transparent color of the picture
    With selectedPicture.ShapeRange.PictureFormat
        .TransparentBackground = True
        .TransparencyColor = RGB(255, 255, 255)
    End With 
End Sub

=================

Sub RemoveShapes()
    ' Select the image you want to remove the background from
    Dim selectedImage As Shape
    Set selectedImage = ActiveSheet.Shapes("Image1")     ' Set the transparent color of the image 

    With selectedImage.PictureFormat
        .TransparentBackground = msoTrue
        .TransparencyColor = RGB(255, 255, 255)
    End With
  End Sub

Thank u

相关问题