吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 217|回复: 3
收起左侧

[经验求助] 【VBA或JS代码】EXCEL/WPS 根据姓名批量插入图片到指定单元格并自动匹配单元格

[复制链接]
kingkiller 发表于 2024-4-27 15:51
30吾爱币
本帖最后由 kingkiller 于 2024-4-27 15:51 编辑

现在有一个需求,需要根据姓名批量插入图片到指定单元格并自动匹配单元格大小
要求支持中文路径、图片格式支持png、jpg、jpeg

最佳答案

查看完整内容

https://wwi.lanzoup.com/iqlZV1wpn55c [mw_shl_code=text,true]Sub InsertPicturesByNameAndResizeCell() Dim ws As Worksheet Dim rng As Range Dim cell As Range Dim picturePath As String Dim targetCell As Range Dim picture As picture Dim pictureWidth As Double Dim pictureHeight As Double ' 设置工作表 Set ws = ThisWorkbook.Sheets("Sheet1") ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zjg121 发表于 2024-4-27 15:51
https://wwi.lanzoup.com/iqlZV1wpn55c

[Plain Text] 纯文本查看 复制代码
Sub InsertPicturesByNameAndResizeCell()
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim picturePath As String
    Dim targetCell As Range
    Dim picture As picture
    Dim pictureWidth As Double
    Dim pictureHeight As Double
      
    ' 设置工作表
    Set ws = ThisWorkbook.Sheets("Sheet1")
      
    ' 假设姓名在A列,图片路径在B列
    Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
      
    ' 遍历范围中的每个单元格
    For Each cell In rng
        picturePath = cell.Offset(0, 1).Value ' 获取图片路径
          
        ' 设置目标单元格(这里假设图片插入到C列的对应行)
        Set targetCell = ws.Cells(cell.Row, "C")
          
        ' 插入图片
        Set picture = ws.Pictures.Insert(picturePath)
          
        ' 将图片移动到目标单元格的位置
        With picture
            .Top = targetCell.Top
            .Left = targetCell.Left
        End With
          
        ' 获取图片的尺寸(不包括边框)
        pictureWidth = picture.Width
        pictureHeight = picture.Height
          
        ' 调整目标单元格的大小以匹配图片尺寸
        ' 注意:这里我们尝试调整单元格大小,但可能不会完全精确
        
        targetCell.RowHeight = pictureHeight  '/ ws.Parent.ScreenUpdatingPixelsPerUnit * 1.3 ' 乘以一个系数以考虑行高的额外空间
        targetCell.ColumnWidth = pictureWidth / 6.25 ' / ws.Parent.ScreenUpdatingPixelsPerUnit
          
        ' 自动调整列宽以适应内容(如果需要更精确匹配)
        'targetCell.EntireColumn.AutoFit
        'targetCell.EntireRow.AutoFit
          
        ' 隐藏图片背后的单元格网格线(可选)
        targetCell.Borders(xlEdgeBottom).LineStyle = xlLineStyleNone
        targetCell.Borders(xlEdgeTop).LineStyle = xlLineStyleNone
        targetCell.Borders(xlEdgeRight).LineStyle = xlLineStyleNone
        targetCell.Borders(xlEdgeLeft).LineStyle = xlLineStyleNone
          
        ' 确保图片位于单元格内并且随单元格大小变化
        With picture
            .Placement = xlMoveAndSize
            .ShapeRange.LockAspectRatio = msoFalse ' 如果需要的话,可以解锁纵横比
        End With
    Next cell
End Sub

aspire168 发表于 2024-4-27 16:06
 楼主| kingkiller 发表于 2024-4-27 16:11
aspire168 发表于 2024-4-27 16:06
描述详细一点,vb给你写一个。

比如一个表格有两列,分别是社区走访人员姓名和图片两列,我现在要根据表格里的姓名来匹配自定义选择的文件夹里的所有图片,进行遍历匹配后插入对应的图片单元格中,并自动嵌入单元格中,类似WPS自带的嵌入单元格效果
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-11 03:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表