2015-08-19 191 views
0

我在宏中編寫程序,它將檢查單元格中圖片的名稱,然後根據圖片的名稱在下一列中添加一個值。Excel的VBA:如何檢查裏面的圖片的名稱的單元格?

如果圖片的名稱在activesheet存在我的程序只檢查。

我的目標是檢查圖片的名稱是否在這個單元格上,然後根據圖片的名稱在下一列添加一個值。非常感謝。

Sub CheckImageName() 
Dim iRowCountShapes As Integer 
Dim sFindShape As String 

iRowCountShapes = 2 


While Sheets("Data").Cells(iRowCountShapes, 1) <> "" 

    If Sheets("Data").Shapes("Rock").Name = "Rock" Then 

     Sheets("Data").Cells(iRowCountShapes, 3) = "Rock" 

    ElseIf Sheets("Data").Shapes("Roll").Name = "Roll" Then 

     Sheets("Data").Cells(iRowCountShapes, 3) = "Roll" 

    Else 

     Sheets("Data").Cells(iRowCountShapes, 3) = "Neither" 

    End If 

    iRowCountShapes = iRowCountShapes + 1 

Wend 

結束子

回答

1

圖像可以作爲obsereved形狀。如果他們是在插入正確命名(與真正的文件名),則可以遍歷它們這樣

Private Sub CommandButton1_Click() 
Dim doc As Worksheet 
Dim spe As Shape 

Set doc = Worksheets(1) 

For i = 1 To doc.Shapes.Count - 1 
    Set spe = doc.Shapes(i) 

    'for named images 
    Debug.Print doc.Shapes(i).Name 

    spe.Select 

    'for linked images 
    Debug.Print GetSourceInfo(spe) 
Next 

End Sub 

Function GetSourceInfo(oShp As Shape) As String 
    On Error GoTo Error_GetSourceInfo 
    GetSourceInfo = oShp.LinkFormat.SourceFullName 
    Exit Function 
Error_GetSourceInfo: 
    GetSourceInfo = "" 
End Function 

這在我的案例顯示了Excel生成imagenames:

  • 圖片1
  • 圖片2
  • 圖片3
+0

感謝@StefanMichev。問題解決了。 – bigbryan

2

照片不會在細胞雷爾,它們是在工作表中上面的圖形層。它更容易只是通過圖片循環,把名字在圖片的左上角下基於單元的相關列:在Excel中

Sub CheckImageName() 
    Dim pic     As Excel.Picture 

    For Each pic In ActiveSheet.Pictures 

     pic.TopLeftCell.Offset(, 2).Value2 = pic.Name 
    Next pic 
End Sub 
1

我必須澄清,我的世界首先,我做什麼都需要ŧ o使事情發揮作用,所以這裏是我獲取對象名稱的低級方法。

錄製宏,複製和粘貼任何地方的對象,停止錄製。刪除粘貼的對象。查看代碼,您剛剛創建的宏在VBA,你會看到對象的名稱。刪除宏。

相關問題