Excel 识别图像名称
我有一张叫做“目录”的单子,里面有很多图片。我想在我选择的特定范围内识别图片的名称(例如“图片15”)。 例如,假设我选择范围(“a1:g10”)。在这个范围内,我可以假设只有一张图片。我想知道这张照片的名字,这样我就可以复制并粘贴到另一张纸上 然而,我尝试了很多方法,但我无法得到图片的名称 下面是我尝试过的代码之一: 如果我删除范围(“a1:g10”),此代码将选择此工作表中的所有图片。然而,这不是我需要的。我必须只选择我想要的特定范围内的图片Excel 识别图像名称,excel,vba,Excel,Vba,我有一张叫做“目录”的单子,里面有很多图片。我想在我选择的特定范围内识别图片的名称(例如“图片15”)。 例如,假设我选择范围(“a1:g10”)。在这个范围内,我可以假设只有一张图片。我想知道这张照片的名字,这样我就可以复制并粘贴到另一张纸上 然而,我尝试了很多方法,但我无法得到图片的名称 下面是我尝试过的代码之一: 如果我删除范围(“a1:g10”),此代码将选择此工作表中的所有图片。然而,这不是我需要的。我必须只选择我想要的特定范围内的图片 Set shplist = Sheets
Set shplist = Sheets("Print_Out").Range("a1:g10")
shplist.Shapes.SelectAll
以下函数将返回在给定范围内找到的第一个形状:
Option Explicit
Public Function GetFirstShapeWithinRange(ByVal Rng As Range, Optional ByVal ShapeType As MsoShapeType = 0) As Shape
Dim ws As Worksheet
Set ws = Rng.Parent
Dim Shp As Shape
For Each Shp In ws.Shapes 'loop through all shapes
If ShapeType = 0 Or Shp.Type = ShapeType Then 'check shape type (if parameter was specified)
If Not Intersect(Rng, ws.Range(Shp.TopLeftCell, Shp.BottomRightCell)) Is Nothing Then 'check if shape and range intersect
Set GetFirstShapeWithinRange = Shp
Exit Function 'exit after first shape was found.
End If
End If
Next Shp
End Function
您可以像下面那样使用它来返回其名称:
Public Sub test()
Dim Pic As Shape
Set Pic = GetFirstShapeWithinRange(Selection, msoPicture) 'msoPicture specifies that the shape we are looking for needs to be a picture.
If Not Pic Is Nothing Then 'check if a picture was found in selection
MsgBox Shp.Name
Else
MsgBox "No picture found in the selected range.", vbExclamation
End If
End Sub
ShapeType
可以根据指定
将考虑以下图片在选定的范围内,因为它相交。
以下函数将返回给定范围内找到的第一个形状:
Option Explicit
Public Function GetFirstShapeWithinRange(ByVal Rng As Range, Optional ByVal ShapeType As MsoShapeType = 0) As Shape
Dim ws As Worksheet
Set ws = Rng.Parent
Dim Shp As Shape
For Each Shp In ws.Shapes 'loop through all shapes
If ShapeType = 0 Or Shp.Type = ShapeType Then 'check shape type (if parameter was specified)
If Not Intersect(Rng, ws.Range(Shp.TopLeftCell, Shp.BottomRightCell)) Is Nothing Then 'check if shape and range intersect
Set GetFirstShapeWithinRange = Shp
Exit Function 'exit after first shape was found.
End If
End If
Next Shp
End Function
您可以像下面那样使用它来返回其名称:
Public Sub test()
Dim Pic As Shape
Set Pic = GetFirstShapeWithinRange(Selection, msoPicture) 'msoPicture specifies that the shape we are looking for needs to be a picture.
If Not Pic Is Nothing Then 'check if a picture was found in selection
MsgBox Shp.Name
Else
MsgBox "No picture found in the selected range.", vbExclamation
End If
End Sub
ShapeType
可以根据指定
将考虑以下图片在选定的范围内,因为它相交。
获取第一个形状名称- 函数
将返回范围内最左边或最上面(getFirstShapeName
)形状的名称。只有当形状的参数<代码> toPrftCys<代码>在范围内时,它才会考虑一个范围内的形状。ByColumns
- 例如,如果
在shp1
中“开始”并且B1
在shp2
中“开始”,那么如果A2
被省略或设置为ByColumns
,它将返回False
的名称(第一行(shp1
)。否则,如果1
设置为ByColumns
,它将返回True
的名称(第一列(shp2
)A
- 附加过程只是测试函数的一个示例
Option Explicit
Function getFirstShapeName( _
aRange As Range, _
Optional ByVal ByColumn As Boolean = False) _
As String
Dim br As Double
Dim bc As Double
If ByColumn Then
br = 0.0000001
bc = 1
Else
br = 1
bc = 0.00001
End If
Dim tmpValue As Double ' Temp Value
tmpValue = aRange.Worksheet.Rows.Count + 1
Dim tlc As Range ' Top Left Cell
Dim cShp As Shape ' Current Shape
Dim tmpShp As Shape ' Temp Shape
Dim cValue As Double ' Current Value
For Each cShp In aRange.Worksheet.Shapes
Set tlc = cShp.TopLeftCell
If Not Intersect(tlc, aRange) Is Nothing Then
cValue = br * tlc.Row + bc * tlc.Column
If cValue < tmpValue Then
tmpValue = cValue
Set tmpShp = cShp
End If
End If
Next cShp
If Not tmpShp Is Nothing Then
getFirstShapeName = tmpShp.Name
End If
End Function
Sub TESTgetFirstShapeName()
Dim s As String
s = getFirstShapeName(Sheet1.Range("A1:C10"), True)
If Len(s) > 0 Then
MsgBox "Found shape '" & s & "'."
Else
MsgBox "No shape found in range."
End If
End Sub
获取第一个形状名称
- 函数
将返回范围内最左边或最上面(getFirstShapeName
)形状的名称。只有当形状的参数<代码> toPrftCys<代码>在范围内时,它才会考虑一个范围内的形状。ByColumns
- 例如,如果
在shp1
中“开始”并且B1
在shp2
中“开始”,那么如果A2
被省略或设置为ByColumns
,它将返回False
的名称(第一行(shp1
)。否则,如果1
设置为ByColumns
,它将返回True
的名称(第一列(shp2
)A
- 附加过程只是测试函数的一个示例
Option Explicit
Function getFirstShapeName( _
aRange As Range, _
Optional ByVal ByColumn As Boolean = False) _
As String
Dim br As Double
Dim bc As Double
If ByColumn Then
br = 0.0000001
bc = 1
Else
br = 1
bc = 0.00001
End If
Dim tmpValue As Double ' Temp Value
tmpValue = aRange.Worksheet.Rows.Count + 1
Dim tlc As Range ' Top Left Cell
Dim cShp As Shape ' Current Shape
Dim tmpShp As Shape ' Temp Shape
Dim cValue As Double ' Current Value
For Each cShp In aRange.Worksheet.Shapes
Set tlc = cShp.TopLeftCell
If Not Intersect(tlc, aRange) Is Nothing Then
cValue = br * tlc.Row + bc * tlc.Column
If cValue < tmpValue Then
tmpValue = cValue
Set tmpShp = cShp
End If
End If
Next cShp
If Not tmpShp Is Nothing Then
getFirstShapeName = tmpShp.Name
End If
End Function
Sub TESTgetFirstShapeName()
Dim s As String
s = getFirstShapeName(Sheet1.Range("A1:C10"), True)
If Len(s) > 0 Then
MsgBox "Found shape '" & s & "'."
Else
MsgBox "No shape found in range."
End If
End Sub
这似乎是一个相当复杂的方法来找出一幅画的名字。如果你只需点击图片,它的名字就会出现在活动屏幕的名字框中。“在一定范围内”到底是什么意思?图片的所有角落都必须在该范围内吗?或者如果图像的一部分(如左上角)覆盖该范围,而图像的另一部分不在该范围内,这就足够了吗?请澄清。@kevin9999这是真的。然而,在我的实际应用中,我将无法点击图片。我的代码必须识别图片的名称(在特定范围内),然后将粘贴复制到另一张图纸并调整其大小。我很难找到图片的名称。@Pᴇʜ图片不需要在所有4个角落。只要在范围内就可以了。“在某个范围内”是可以找到其中一张图片的地方。在这个范围内,我需要识别图片的名称。你可以放心地假设每个选定范围内只有一张图片。这似乎是一个相当复杂的查找图片名称的方法。如果你只需点击图片,它的名字就会出现在活动屏幕的名字框中。“在一定范围内”到底是什么意思?图片的所有角落都必须在该范围内吗?或者如果图像的一部分(如左上角)覆盖该范围,而图像的另一部分不在该范围内,这就足够了吗?请澄清。@kevin9999这是真的。然而,在我的实际应用中,我将无法点击图片。我的代码必须识别图片的名称(在特定范围内),然后将粘贴复制到另一张图纸并调整其大小。我很难找到图片的名称。@Pᴇʜ图片不需要在所有4个角落。只要在范围内就可以了。“在某个范围内”是可以找到其中一张图片的地方。在这个范围内,我需要识别图片的名称。您可以放心地假设每个选定范围内只有一张图片。谢谢您的代码。我可以澄清一下吗?在IF条件下的函数中,为什么需要在IF条件中包含ShapeType=0?它有什么用途?我还意识到,如果我在Rng中省略“.Parent”,代码将不起作用。我可以知道为什么吗?@user13316884函数中的
ShapeType
参数是可选的
,这意味着如果在函数调用中省略它,比如Set Pic=getfirstshapewithinnrange(Selection)
它应该可以找到任何类型的形状,而不仅仅是图片。因此,我们需要ShapeType=0
(这意味着没有指定类型).Parent
提供对象的父对象。因为范围是工作表范围的子项。父项将返回范围所在的工作表。因此,Set ws=Rng.Parent
用于获取Rng
的工作表,因为这是我们需要搜索形状的工作表。@user13316884如果这回答了您的问题,请将其投票/标记为已解决:谢谢您