0
已嘗試寫入VBA以將圖片插入Excel中的特定列(F) 圖片文件的鏈接列在C列中 我研究了所有我可以聯機的,似乎找到了一種方式,它可以工作,但有一件事情一直在失敗! 如果文件是有作品的話,如果C列中的單元格保留空白,它會將相應的單元格留在列F空白,但如果鏈接包含文件名不存在,則停止。 如果文件名已損壞=已聲明但不存在,我希望它只留下單元格空白並移至下一個單元格。可以看到我做錯了什麼?使用Len(Dir檢查圖片文件並將其插入到excel文件中 - 只是不工作
Appriciate的幫助。 中號
Sub Insertpicture()
Dim myPict As Picture
Dim curWks As Worksheet
Dim myRng As Range
Dim myCell As Range
Set curWks = Sheets(1)
curWks.Pictures.Delete
With curWks
Set myRng = .Range("c2", .Cells(.Rows.Count, "c").End(xlUp))
End With
For Each myCell In myRng.Cells
If Trim(myCell.Value) = "" Then
‘do nothing, move on
ElseIf Len(Dir(myCell.Value)) = 0 Then
’here I want to leave the cell empty and just move on to check next cell
Else
With myCell.Offset(0, 3)
Set myPict = myCell.Parent.Pictures.Insert(myCell.Value)
myPict.Top = .Top
myPict.Width = .Width
myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With
End If
Next myCell
End Sub
嗨非常感謝您的意見。這工作非常完美。 (我在示例中留下了錯誤處理程序)而不是讓一個包含圖片路徑的單元格,我想將「路徑,單元格值和文件類型」寫入vba中。但是編碼有問題。在設置strpath和strfile之後,我將picpath聲明爲「picpath = strpath&myCell.Value&strfile」,然後是「Set myPict = myCell.Parent.Pictures.Insert(picpath)」,但這不起作用。我可以再次強加給你這個嗎? – roseq
可以看到這可能需要不同的vba。 – roseq
@roseq - 使用'FilesystemObject'的[BuildPath](https://msdn.microsoft.com/en-us/library/z0z2z1zt(v = vs.84).aspx)方法而不是串聯。 – Comintern