插入图片与AddPicture给我一个“参数不是可选的”错误

时间:2019-11-19 03:57:31

标签: excel vba

我一直在尝试编写代码,以根据单元格的宽度将图片插入到单元格中(保持宽高比并更改单元格的高度以适合图片)。

它给了我编译错误

  

“参数不是可选的”

在“ AddPicture”行。

Private Sub btnCancel_Click()

Dim emptyRow As Long
Const msoFileDialogFilePicker As Long = 3
Dim fileExplorer As fileDialog
Dim sFilePath As String
Dim Picture As Shape

emptyRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row


If MsgBox("Would you like to add a picture?", vbYesNo, "Add a Picture") = vbYes Then

    Set fileExplorer = Application.fileDialog(msoFileDialogFilePicker)

    With fileExplorer

        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Images", "*.png; *.jpg; *.jpeg", 1
        .Show
        If .SelectedItems.Count = 0 Then

            MsgBox "Canceled"

        Else

            sFilePath = .SelectedItems.Item(1)
            Set Picture = Sheet1.Shapes.AddPicture(sFilePath, msoFalse, msoCTrue)
            With Picture

                .LockAspectRatio = True
                .Left = Sheet1.Cells(emptyRow, 6).Left
                .Top = Sheet1.Cells(emptyRow, 6).Top
                .Width = Sheet1.Range(Cells(emptyRow, 6)).Width
                Sheet1.Rows(Picture.TopLeftCell.Row).RowHeight = .Height

            End With
        End If

    End With

    Exit Sub

End If

End Sub

1 个答案:

答案 0 :(得分:-1)

Set Picture = Sheet1.Shapes.AddPicture(sFilePath, msoFalse, msoCTrue, 100, 100, 100, 100)