|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Private Sub CommandButton4_Click()
- If ActiveSheet.ProtectContents Then MsgBox "工作表已保护,本程序拒绝执行!", 64, "提示": Exit Sub
- Dim TypeName%, str$ '用于判断图片格式的变量
- Dim fd As FileDialog, folder$ '文件路径对象和变量
- Dim rng As Range, cell As Range '选择的区域单元格对象
- Dim r As Range '选择的区域单元格对象 此处用于判断选取的区域是否为空
- Dim j%, i%, s$, n%, k%
- Dim w!, m!
- '——————————————————————————————————————————————
- Restar:
- Set r = Selection.Find("*", , , , , xlPrevious)
- If r Is Nothing Then MsgBox "不能选择空白区", 64, "提示": Exit Sub
- Set r = Nothing
- TypeName = Application.InputBox("输入1:插入GIF 图片;" + Chr(10) + "输入2:插入PNG 图片;" + Chr(10) + "输入3:插入JPG 图片;" + Chr(10) + "输入4:插入JPEG图片。", "图片格式", 3, , , , , 1)
-
- If TypeName = False Then
- Exit Sub
- ElseIf TypeName < 1 Or TypeName > 4 Then
- MsgBox "输入错误": GoTo Restar
- End If
-
- str = VBA.Choose(TypeName, "*.gif", "*.png", "*.jpg", "*.jpeg") '根据输入的数字决定选用图片格式
- Set rng = Application.Intersect(ActiveSheet.UsedRange, Selection)
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- If fd.Show = -1 Then folder = fd.SelectedItems(1) Else Exit Sub '如果选择了路径提取路径全名否则退出程序
- On Error Resume Next
- 'rng.ClearComments '清除选区的所有批注
- Err.Clear
-
- i = ActiveCell.RowHeight
- star:
- j = Application.InputBox("请输入你希望照片显示的高度," & Chr(10) & "当前单元格高度为" & i & ",请根据需要按比例输入新高度。" & Chr(10) & "程序将把单元格的高度与宽度调整为同样大小。", "【确认照片高度】", i, , , , , 1)
- If Err <> 0 Then Err.Clear: MsgBox "输入不规划,请重新输入": GoTo star
- 'If j = False Then Exit Sub
- If j <= 0 Then j = i
- Application.ScreenUpdating = False
-
-
- For Each cell In rng '遍历选区
- cell.Activate
-
- If cell <> "" Then
-
- If Dir(folder & "" & cell.Text & Mid(str, 2)) <> "" Then
- 'ActiveSheet.Pictures.Insert(folder & "" & cell.Text & Mid(str, 2)).Select
- ActiveSheet.Shapes.AddPicture(folder & "" & cell.Text & Mid(str, 2), False, True, 0, 0, -1, -1).Select
-
-
- With Selection
- If j = 0 Then
- .Placement = xlMoveAndSize '图片大小、位置都随单元格而变
- .ShapeRange.LockAspectRatio = msoFalse '不锁定图片纵横比
-
- .Top = ActiveCell.Offset(0, 1).Top + 2
- .Left = ActiveCell.Offset(0, 1).Left + 2
- .Height = ActiveCell.Height - 2
- .Width = ActiveCell.Width - 2
- Columns(ActiveCell.Column).AutoFit
-
-
- Else
- .ShapeRange.LockAspectRatio = msoTrue '锁定图片纵横比
- .Top = ActiveCell.Offset(0, 1).Top + 2
- .Left = ActiveCell.Offset(0, 1).Left + 2
- .Height = j
- '.Width = ActiveCell.Width
- w = .Width
-
- ActiveCell.RowHeight = j + 4
- 'ActiveCell.Offset(0, 1).ColumnWidth = CInt(((w + 2) * 4.374 / 27.682) + 1)
-
- cell.Offset(1, 0).Select '选择下一单元格
- If w > m Then m = w
-
- End If
- End With
-
-
-
-
- Else
- s = s + cell.Text & Mid(str, 2) & Chr(10)
- n = n + 1 '记录未找到的图片数
- 'ActiveCell.RowHeight = j + 2
- cell.Offset(1, 0).Select '选择下一单元格
- End If
- k = k + 1 '记录非空单元格个数
- End If
- Next
-
- If m > 0 Then ActiveCell.Offset(0, 1).ColumnWidth = m / 6.016887
- '6.016887
- If n = 0 Then
- MsgBox "完成!图片已经全部导入!", 64, "提示"
- Else
- MsgBox "完成!" & Chr(10) & "需要导入图片" & k & "张" & Chr(10) & "成功导入图片" & k - n & "张" & Chr(10) & "未找到对应图片" & n & "张:" & Chr(10) & s, 64, "提示"
- End If
- Set rng = Nothing
- Application.ScreenUpdating = True
-
- '反馈
- Me.Frame1.Caption = "反馈:导入完成!"
- Me.Frame1.Font.Bold = True
- Me.Frame1.ForeColor = &HFF00&
- Me.Frame1.Font.Size = 10
- End Sub
复制代码
这二个是控件调用的 稍微改一下就好了 |
|