使用VBA在EXCEL中插入图片自定义函数(网络URL/本地URL)

Sub InsertPic(ByVal 图片链接 As String, ByVal 插入图片表名 As String, ByVal 插入图片单元格地址 As String)
    On Error Resume Next
    Dim rng As Range
    Set rng = Sheets(插入图片表名).Range(插入图片单元格地址)
    With Sheets(插入图片表名).Pictures.Insert(图片链接)
        .ShapeRange.LockAspectRatio = msoFalse '取消锁定比例
        .Width = rng.Width - 3
        .Height = rng.Height - 3
        .Top = rng.Top + 1.5
        .Left = rng.Left + 1.5
        If Err.Number <> 0 Then
            .Delete
            Err.Clear
        End If
    End With
    Set rng = Nothing
End Sub
Sub InsertPic2(ByVal 图片链接 As String, ByVal 插入图片表名 As String, ByVal 插入图片单元格地址 As String)
    On Error Resume Next
    Dim rng As Range
    Set rng = Sheets(插入图片表名).Range(插入图片单元格地址)
    With Sheets(插入图片表名).Shapes.AddShape(msoShapeRectangle, rng.Left + 1.5, rng.Top + 1.5, rng.Width - 3, rng.Height - 3).Select '插入一个矩形框
        Selection.ShapeRange.Fill.UserPicture 图片链接 '给框内填充图片
        Selection.ShapeRange.Line.Visible = msoFalse '无边框
        If Err.Number <> 0 Then
            Selection.Delete
            Err.Clear
        End If
    End With
    Set rng = Nothing
End Sub
Sub 调用()
    Call InsertPic("http://www.baidu.com/img/bd_logo1.png", ActiveSheet.Name, "c1")
End Sub

你可能感兴趣的:(VBA)