|
楼主 |
发表于 2024-2-22 13:29
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
目前可以实现上传图片到指定单元格,单击图片可以放大再次单击恢复原样,但是插入的图片不能铺满,而且合并后的单元格无效;求大神修改
Sub 插入图片()
Dim filenames As String
Dim filefilter1 As String
filefilter1 = ("所有图片文件(*.jpg;*.bmp;*.png;*.gif),*.jpg;*.bmp;*.png;*.gif") '所有图片文件后面的括号为中文括号
filenames = Application.GetOpenFilename(filefilter1, , "请选择一个图片文件", , MultiSelect:=False)
'没有选中文件时,做容错处理
If filenames = "False" Then Exit Sub
'插入图片到指定的单元格
Sheet1.Pictures.Insert(filenames).Cut
Sheet1.Pictures.Paste.Select
'图片自适应单元格大小
On Error Resume Next
Dim picW As Single, picH As Single
Dim cellW As Single, cellH As Single
Dim rtoW As Single, rtoH As Single
cellW = ActiveCell.Width
cellH = ActiveCell.height
picW = Selection.ShapeRange.Width
picH = Selection.ShapeRange.height
rtoW = cellW / picW * 0.95
rtoH = cellH / picH * 0.95
If rtoW < rtoH Then
Selection.ShapeRange.ScaleWidth rtoW, msoFalse, msoScaleFromTopLeft
Else
Selection.ShapeRange.ScaleHeight rtoH, msoFalse, msoScaleFromTopLeft
End If
picW = Selection.ShapeRange.Width
picH = Selection.ShapeRange.height
Selection.ShapeRange.IncrementLeft (cellW - picW) / 2
Selection.ShapeRange.IncrementTop (cellH - picH) / 2
With Selection
.OnAction = "test"
.Name = ActiveCell.Address(0, 0)
.AlternativeText = Empty
End With
[a1].Select
End Sub
|
|