This article brings you relevant knowledge about excel. It mainly organizes related issues on how to query pictures by name, including how to insert pictures from one worksheet to another. A worksheet, based on the picture names in column A of the data table, inserting photos from the photo table into column B of the data table in batches, etc. Let’s take a look at it together. I hope it will be helpful to everyone.
Related learning recommendations: excel tutorial
How to insert pictures from one worksheet to another worksheet? for example.
As shown below:
A workbook has two worksheets.
The worksheet that stores photos is named [Photo], and the worksheet that needs to insert pictures is named [Data].
Now we need to batch insert photos from the [Photo] table into column B of the [Data] table based on the picture names in column A of the [data] table...
The sample animation is as follows :
......
To achieve such a function, 3 lines of code are actually enough.
The code is as follows:
Sub InsertPicFromSheet() Dim rngData As Range, rngPicName As Range For Each rngData In Range("a2", Cells(Rows.Count, 1).End(3)) Set rngPicName = Sheets("照片").Cells.Find(rngData.Value, , , xlWhole) '使用Find方法在照片表完整匹配姓名 If Not rngPicName Is Nothing Then rngPicName.Offset(0, 1).Copy rngData.Offset(0, 1) '如果有找到对应的姓名,则将照片复制粘贴到目标位置 Next End Sub
However...
The biggest problem with the above code is that the data table does not delete the old pictures. If the program is run repeatedly, the pictures will be Cumulatively, in order to solve this problem, we need to add two more lines of code.
The code is modified as follows:
Sub InsertPicFromSheet() Dim shp As Shape, rngData As Range, rngPicName As Range For Each shp In ActiveSheet.Shapes '删除活动工作表原有照片 If shp.Type = 13 Then shp.Delete Next For Each rngData In Range("a2", Cells(Rows.Count, 1).End(3)) Set rngPicName = Sheets("照片").Cells.Find(rngData.Value, , , xlWhole) '使用Find方法在照片表的完整匹配姓名 If Not rngPicName Is Nothing Then rngPicName.Offset(0, 1).Copy rngData.Offset(0, 1) '如果有找到对应的姓名,则将照片复制粘贴到目标位置 Next End Sub
The above code uses a one-size-fits-all approach to delete old images.
But...
Although this code is enough for friends with good VBA foundation to deal with most problems with slight modifications, for novices, it is obviously Not friendly enough...
For example...
1. The name of the photo is fixed in column A of the data table. In actuality, it is probably not column A. I am right.
2. The position where the photo is placed is fixed in the cell where the name column is moved one column to the right. In actual situation, of course, this may not be the case. What I said is still right.
3. The worksheet that stores photos is fixedly set to sheets ("photos") in the code. In actuality, this is definitely not the case. I am wise...
4. The code is not set. The size of the cell is adapted to the size of the picture, I...
The code is modified as follows:
Sub InsertPicFromSheet2() 'ExcelHome VBA编程学习与实践 by:看见星光 Dim rngData As Range, rngWhere As Range, cll As Range Dim rngPicName As Range, rngPic As Range, rngPicPaste As Range Dim shp As Shape, sht As Worksheet, bln As Boolean Dim strWhere As String, strPicName As String, strPicShtName As String Dim x, y As Long, lngYesCount As Long, lngNoCount As Long 'On Error Resume Next Set rngData = Application.InputBox("请选择应插入图片名称的单元格区域", Type:=8) '用户选择需要插入图片的名称所在单元格范围 Set rngData = Intersect(rngData.Parent.UsedRange, rngData) 'intersect语句避免用户选择整列单元格,造成无谓运算的情况 If rngData Is Nothing Then MsgBox "选择的单元格范围不存在数据!": Exit Sub strWhere = InputBox("请输入放置图片偏移的位置,例如上1、下1、左1、右1", , "右1") '用户输入图片相对单元格的偏移位置 If Len(strWhere) = 0 Then Exit Sub x = Left(strWhere, 1) '偏移的方向 If InStr("上下左右", x) = 0 Then MsgBox "你未输入偏移方位。": Exit Sub y = Val(Mid(strWhere, 2)) '偏移的值 Select Case x Case "上" Set rngWhere = rngData.Offset(-y, 0) Case "下" Set rngWhere = rngData.Offset(y, 0) Case "左" Set rngWhere = rngData.Offset(0, -y) Case "右" Set rngWhere = rngData.Offset(0, y) End Select strPicShtName = InputBox("请输入存放图片的工作表名称", , "照片") For Each sht In Worksheets If sht.Name = strPicShtName Then bln = True Next If bln <> True Then MsgBox "未找到保存图片的工作表:" & strPicShtName & vbCrLf & "程序退出。": Exit Sub Application.ScreenUpdating = False rngData.Parent.Select For Each shp In ActiveSheet.Shapes '如果旧图片存放在目标图片存放范围则删除 If Not Intersect(rngWhere, shp.TopLeftCell) Is Nothing Then shp.Delete Next x = rngWhere.Row - rngData.Row y = rngWhere.Column - rngData.Column '偏移的纵横坐标 For Each cll In rngData '遍历选择区域的每一个单元格 strPicName = cll.Text '图片名称 If Len(strPicName) Then '如果单元格存在值 Set rngPicName = Sheets(strPicShtName).Cells.Find(cll.Value, , , xlWhole) '使用Find方法在照片表完整匹配姓名 If Not rngPicName Is Nothing Then Set rngPicPaste = cll.Offset(x, y) '粘贴图片的单元格 Set rngPic = rngPicName.Offset(0, 1) '保存图片的单元格 lngYesCount = lngYesCount + 1 '累加找到结果的个数 If lngYesCount = 1 Then '设置放置图片单元格的行高和列宽,以适应图片的大小 rngPicPaste.RowHeight = rngPic.RowHeight rngPicPaste.ColumnWidth = rngPic.ColumnWidth End If rngPicName.Offset(0, 1).Copy rngPicPaste '如果有找到对应的姓名,则将照片复制粘贴到目标位置 Else lngNoCount = lngNoCount + 1 '累加未找到结果的个数 End If End If Next Application.ScreenUpdating = True MsgBox "共处理成功" & lngYesCount & "个对象,另有" & lngNoCount & "个非空单元格未找到对应的图片名称。" End Sub
The above code solves the three common problems we mentioned earlier...
Ran...Three but...
There are still some unresolved problems that may arise in practical applications...
For example...
1. How to solve the problem of pictures and Data source linkage? When the data source image changes, the data table image also changes automatically? Well, in addition to re-running the program, you can also use the activation event of the worksheet, or use activesheet.chartobjects.add...
2. How to set the size of the picture to fit the cell instead of adjusting the size of the cell Adapt to the picture?
Related learning recommendations: excel tutorial
The above is the detailed content of How to easily query pictures by name in Excel. For more information, please follow other related articles on the PHP Chinese website!