search
HomeTopicsexcelHow to easily query pictures by name in Excel

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.

How to easily query pictures by name in Excel

Related learning recommendations: excel tutorial

How to insert pictures from one worksheet to another worksheet? for example.

As shown below:

How to easily query pictures by name in Excel

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 :

How to easily query pictures by name in Excel

......

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
&#39;如果旧图片存放在目标图片存放范围则删除
If Not Intersect(rngWhere, shp.TopLeftCell) Is Nothing Then shp.Delete
Next
x = rngWhere.Row - rngData.Row
y = rngWhere.Column - rngData.Column
&#39;偏移的纵横坐标
For Each cll In rngData
&#39;遍历选择区域的每一个单元格
strPicName = cll.Text
&#39;图片名称
If Len(strPicName) Then
&#39;如果单元格存在值
Set rngPicName = Sheets(strPicShtName).Cells.Find(cll.Value, , , xlWhole)
&#39;使用Find方法在照片表完整匹配姓名
If Not rngPicName Is Nothing Then
Set rngPicPaste = cll.Offset(x, y)
&#39;粘贴图片的单元格
Set rngPic = rngPicName.Offset(0, 1)
&#39;保存图片的单元格
lngYesCount = lngYesCount + 1
&#39;累加找到结果的个数
If lngYesCount = 1 Then
&#39;设置放置图片单元格的行高和列宽,以适应图片的大小
rngPicPaste.RowHeight = rngPic.RowHeight
rngPicPaste.ColumnWidth = rngPic.ColumnWidth
End If
rngPicName.Offset(0, 1).Copy rngPicPaste
&#39;如果有找到对应的姓名,则将照片复制粘贴到目标位置
Else
lngNoCount = lngNoCount + 1
&#39;累加未找到结果的个数
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!

Statement
This article is reproduced at:Excel Home. If there is any infringement, please contact admin@php.cn delete
MEDIAN formula in Excel - practical examplesMEDIAN formula in Excel - practical examplesApr 11, 2025 pm 12:08 PM

This tutorial explains how to calculate the median of numerical data in Excel using the MEDIAN function. The median, a key measure of central tendency, identifies the middle value in a dataset, offering a more robust representation of central tenden

Google Spreadsheet COUNTIF function with formula examplesGoogle Spreadsheet COUNTIF function with formula examplesApr 11, 2025 pm 12:03 PM

Master Google Sheets COUNTIF: A Comprehensive Guide This guide explores the versatile COUNTIF function in Google Sheets, demonstrating its applications beyond simple cell counting. We'll cover various scenarios, from exact and partial matches to han

Excel shared workbook: How to share Excel file for multiple usersExcel shared workbook: How to share Excel file for multiple usersApr 11, 2025 am 11:58 AM

This tutorial provides a comprehensive guide to sharing Excel workbooks, covering various methods, access control, and conflict resolution. Modern Excel versions (2010, 2013, 2016, and later) simplify collaborative editing, eliminating the need to m

How to convert Excel to JPG - save .xls or .xlsx as image fileHow to convert Excel to JPG - save .xls or .xlsx as image fileApr 11, 2025 am 11:31 AM

This tutorial explores various methods for converting .xls files to .jpg images, encompassing both built-in Windows tools and free online converters. Need to create a presentation, share spreadsheet data securely, or design a document? Converting yo

Excel names and named ranges: how to define and use in formulasExcel names and named ranges: how to define and use in formulasApr 11, 2025 am 11:13 AM

This tutorial clarifies the function of Excel names and demonstrates how to define names for cells, ranges, constants, or formulas. It also covers editing, filtering, and deleting defined names. Excel names, while incredibly useful, are often overlo

Standard deviation Excel: functions and formula examplesStandard deviation Excel: functions and formula examplesApr 11, 2025 am 11:01 AM

This tutorial clarifies the distinction between standard deviation and standard error of the mean, guiding you on the optimal Excel functions for standard deviation calculations. In descriptive statistics, the mean and standard deviation are intrinsi

Square root in Excel: SQRT function and other waysSquare root in Excel: SQRT function and other waysApr 11, 2025 am 10:34 AM

This Excel tutorial demonstrates how to calculate square roots and nth roots. Finding the square root is a common mathematical operation, and Excel offers several methods. Methods for Calculating Square Roots in Excel: Using the SQRT Function: The

Google Sheets basics: Learn how to work with Google SpreadsheetsGoogle Sheets basics: Learn how to work with Google SpreadsheetsApr 11, 2025 am 10:23 AM

Unlock the Power of Google Sheets: A Beginner's Guide This tutorial introduces the fundamentals of Google Sheets, a powerful and versatile alternative to MS Excel. Learn how to effortlessly manage spreadsheets, leverage key features, and collaborate

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.