我想检查单元格中图片的名称,然后根据图片的名称在下一列中输入一个值。
我的程序只检查图片的名称是否存在于活动工作表中。
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
End Sub
Run Code Online (Sandbox Code Playgroud)
Excel 中的图像可以被视为形状。如果它们在插入时命名正确(使用真实文件名),那么您可以像这样迭代它们
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
Run Code Online (Sandbox Code Playgroud)
在我的例子中,显示了 excel 生成的图像名称: