2016-10-25 80 views
0

我正在學習excel vba,並且我堅持了幾個小時這個錯誤。我在這裏閱讀了一些解決方案,但我無法修復。此代碼獲取相片路徑,並且插入在單元格A1excel vba對象定義的錯誤

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

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

End Sub 

這具有應用程序中定義或對象定義的錯誤的評論框的照片。我檢查「TheFile」,它返回正確的字符串,它是文件路徑

+0

你的代碼的作品出現錯誤對我來說,只要'TheFile'具有完整路徑_and_圖片的名稱 – user3598756

+0

是的,如果我粘貼一個字符串而不是從單元格值中取路徑。它的工作原理 –

+0

您嘗試在Range(「A1」),AddComment和Range(「A1」)之間添加'Range(「A1」)。Comment.Visible = True' Comment.Shape.Fill.UserPicture TheFile'? –

回答

0

也許你需要使用括號,或者添加註釋althoug有alredy是一條評論

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 
+0

謝謝。我添加了括號。仍然存在相同的錯誤 –

+0

您是否嘗試添加If語句我編寫befor添加評論? '如果範圍(「A1」)。評論是沒有那麼範圍(「A1」)。AddComment' – FatTony

+0

對我來說代碼完美的作品。你確定你從正確的單元格走的路?單元格(2,14)是單元格N2 – FatTony

相關問題