需要帮助通过图片上传代码

时间:2018-09-24 12:55:59

标签: excel-vba

Sub Cont_attachthumb()
Dim PicFile As FileDialog
With Sheet2
  Set PicFile = Application.FileDialog(msoFileDialogFilePicker)
  With PicFile
    .Title = "Select A Content Picture"
    .Filters.Add "All Picture Files", ".jpg, *jpeg, *.gif, *.png, *bmp", 1

    If .Show <> -1 Then GoTo NoSelection

    Sheet2.Range("R11").Value = .SelectedItems(1) 'Put File Name in R11
  End With
If .Range("B3").Value = False Then .Range("M" & Sheet2.Range("B2").Value).Value = .Range("R11").Value

Cont_displaythum

NoSelection:
  End With
End Sub

我收到运行时91错误 对象变量或带有块变量的未设置错误

here is the error

2 个答案:

答案 0 :(得分:1)

您正在设置2条With语句:    1. With Sheet2    2. With PicFile 即使它们没有连接或没有必要,也可以在另一个内部。

第二部分,If .Range("B3").Value = False Then .Range("M" & Sheet2.Range("B2").Value).Value = .Range("R11").Value最有可能与Sheet2对象相关,因此它必须位于With Sheet2语句中,因此@JNevill在其注释中写道,您需要移动此行之后的End With

下面的代码注释中的更多说明:

修改后的代码

Sub Cont_attachthumb()

Dim PicFile As FileDialog

Set PicFile = Application.FileDialog(msoFileDialogFilePicker)
With PicFile
    .Title = "Select A Content Picture"
    .Filters.Add "All Picture Files", "*.jpg, *jpeg, *.gif, *.png, *bmp", 1
    If .Show <> -1 Then GoTo NoSelection

    Sheet2.Range("R11").Value = .SelectedItems(1) 'Put File Name in R11
End With

With Sheet2
    If .Range("B3").Value = False Then .Range("M" & .Range("B2").Value).Value = .Range("R11").Value
    ' moved this ^ row above the End With
End With

Cont_displaythum ' <-- I hope this is a Sub defined somewhere else

NoSelection:

End Sub

答案 1 :(得分:0)

分隔符不是“,”而是“;”好像是。

更改此

.Filters.Add "All Picture Files", ".jpg, *jpeg, *.gif, *.png, *bmp", 1

.Filters.Add "All Picture Files", "*.jpg; *.jpeg; *.gif; *.png; *bmp", 1