简体   繁体   中英

excel vba object-defined error

I am learning excel vba and I´m stuck with this error for a few hours. I´ve read some solutions here but I can´t fix it. This code gets a photo path and inserts the photo in comment-box of cell A1

Sub test()
    Dim TheFile As String
    TheFile = Cells(2, 14).Value

    Range("A1").AddComment
    Range("A1").Comment.Shape.Fill.UserPicture TheFile

End Sub

This has application defined or object defined error. I check "TheFile", it returns the correct string which is the file path

Maybe you need to use brackets, or the error occurs on adding a comment althoug there alredy is a comment

Sub test()
    Dim TheFile As String
    TheFile = Cells(2, 14).Value

    'Only adds a comment if cell don´t already have one
    If Range("A1").Comment Is Nothing Then Range("A1").AddComment           
    Range("A1").Comment.Shape.Fill.UserPicture (TheFile)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM