在Excel VBA中删除先前的图像。

huangapple go评论63阅读模式
英文:

Delete the previous image in excel vba

问题

这里,当我点击数值时,图片会正确显示,但问题是当我点击图片时,之前的图片应该被删除。我尝试了删除的代码(使用注释进行了突出显示),但它不起作用。如果我做了任何微小的错误,请有人能纠正我吗?

Private Sub ShowValues()
Dim a As String
Dim Var As String
Dim Var2 As String
Dim insert_path As String
Dim pic As Picture

'删除代码
For Each pic In ActiveSheet.Pictures
    If Not Application.Intersect(pic.TopLeftCell, Range("H10:R24")) Is Nothing Then
        pic.Delete
    End If
Next pic

Range("C1").Value = ActiveCell.Value
'a = Range("C1").Value
Var = "********" '驱动路径
 'MsgBox (Range("B" & j).Value)
 Var2 = Range("C1").Value
 insert_path = Var & "\" & Var2

  Call ActiveSheet.Shapes.AddPicture(insert_path, _
     msoCTrue, msoCTrue, 400, 160, 800, 600)
End Sub

注意:这是代码的一部分,用于删除之前插入的图片。

英文:

Here the Image is getting populated correctly when i click the value, but the issue is when i click on the image, the previous image has to be deleted. I tried a deleted code(highlighted using comments) but that is not working. can someone please correct me if am doing any minute mistake.

Private Sub ShowValues()
Dim a As String
Dim Var As String
Dim Var2 As String
Dim insert_path As String
Dim pic As Picture


'Delete Code
For Each pic In ActiveSheet.Pictures
    If Not Application.Intersect(pic.TopLeftCell, Range("H10:R24")) Is Nothing Then
        pic.Delete
    End If
Next pic




Range("C1").Value = ActiveCell.Value
'a = Range("C1").Value
Var = "********" 'drivepath
 'MsgBox (Range("B" & j).Value)
 Var2 = Range("C1").Value
 insert_path = Var & "\" & Var2

  Call ActiveSheet.Shapes.AddPicture(insert_path, _
     msoCTrue, msoCTrue, 400, 160, 800, 600)
End Sub

答案1

得分: 3

以下是翻译好的部分:

你可以使用多种方式来识别一个形状。通过单元格范围或其Top/Left属性来识别它的问题是,如果用户移动图像或调整某些单元格,这些属性可能会发生变化,因此最终这不是可靠的。

我建议在插入图片之后为包含图片的形状命名。现在,在插入之前,检查是否已经存在一个具有该名称的形状,并将其删除(我在自由地重命名了您的变量):

Private Sub ShowValues()
    Const imageShapeName = "MyGreatShapeHoldingAnImage" ' 任何你想要的名称。
    
    Dim path As String, imageName As String, fullName As String
    
    With ActiveSheet
        .Range("C1").Value = ActiveCell.Value
        path = "********" ' 驱动路径
        imageName = Range("C1").Value
        fullName = path & "\" & imageName
    
        Dim pic As Shape
        On Error Resume Next
        Set pic = .Shapes(imageShapeName)
        On Error GoTo 0
        If Not pic Is Nothing Then pic.Delete
    
        Call .Shapes.AddPicture(fullName, msoCTrue, msoCTrue, 400, 160, 800, 600)
        Set pic = .Shapes(.Shapes.Count)
        pic.Name = imageShapeName
    End With
End Sub

如果您需要进一步的帮助,请随时提问。

英文:

You can use several ways to identify a shape. Problem by identify it by the cell Range or it's Top/Left property is that this might change if the user moves the image around or resize some cells, so at the end of the day this is not reliable.

I would suggest you name the shape holding the picture after inserting it. Now before you insert it, check if there is already a shape with that name and remove it (I took the freedom to rename your variables):

Private Sub ShowValues()
    Const imageShapeName = "MyGreatShapeHoldingAnImage"  ' Whatever you want.
    
    Dim path As String, imageName As String, fullName As String
    
    With ActiveSheet
        .Range("C1").Value = ActiveCell.Value
        path = "********" 'drivepath
        imageName = Range("C1").Value
        fullName = path & "\" & imageName
    
        Dim pic As Shape
        On Error Resume Next
        Set pic = .Shapes(imageShapeName)
        On Error GoTo 0
        If Not pic Is Nothing Then pic.Delete
    
        Call .Shapes.AddPicture(fullName, msoCTrue, msoCTrue, 400, 160, 800, 600)
        Set pic = .Shapes(.Shapes.Count)
        pic.Name = imageShapeName
    End With
End Sub

huangapple
  • 本文由 发表于 2023年5月24日 23:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定