VB6插入图片到 Excel 2007 指定单元格
答案:3 悬赏:50
解决时间 2021-01-14 02:08
- 提问者网友:谁的错
- 2021-01-13 02:04
VB6插入图片到 Excel 2007 指定单元格
最佳答案
- 二级知识专家网友:拾荒鲤
- 2021-01-13 02:57
可以用vb6代码完成:
1、先编制表格:
2、在表格里编写触发宏代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim photoname As String
If Target.Row = 3 And Target.Column > 3 And Target.Column < 6 Then
On Error Resume Next '忽略错误继续执行VBA代码,避免出现错误消息
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each shp In Sheets("查询表").Shapes
If shp.Type <> 8 And shp.Type <> 12 Then
shp.Delete
End If
Next
photoname = Cells(3, 4) & ".JPG"
Cells(3, "L").Select
ActiveSheet.Pictures.Insert(ActiveWorkbook.Path & "照片" & photoname).Select '当前文件所在目录下以单元内容为名称的.jpg图片
With Selection
ta = Range(Cells(3, "L").MergeArea.Address).Height '单元高度
tb = Range(Cells(3, "L").MergeArea.Address).Width '单元宽度
tc = .Height '图片高度
td = .Width '图片宽度
tm = Application.WorksheetFunction.Min(ta / tc, tb / td) '单元与图片之间长宽差异比例的最小值
.Top = ActiveCell.Top + 2
.Left = ActiveCell.Left + 1
.Height = .Height * tm * 0.98 '按比例调整图片宽度
.Width = .Width * tm * 0.98 '按比例调整图片高度
End With
Cells(3, 4).Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
3、在当前目录下建个名为”照片“的子目录,里面存有以姓名为名称的.jpg格式的照片
4、在姓名后单元输入姓名后,就能自动插入图片了
1、先编制表格:
2、在表格里编写触发宏代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim photoname As String
If Target.Row = 3 And Target.Column > 3 And Target.Column < 6 Then
On Error Resume Next '忽略错误继续执行VBA代码,避免出现错误消息
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each shp In Sheets("查询表").Shapes
If shp.Type <> 8 And shp.Type <> 12 Then
shp.Delete
End If
Next
photoname = Cells(3, 4) & ".JPG"
Cells(3, "L").Select
ActiveSheet.Pictures.Insert(ActiveWorkbook.Path & "照片" & photoname).Select '当前文件所在目录下以单元内容为名称的.jpg图片
With Selection
ta = Range(Cells(3, "L").MergeArea.Address).Height '单元高度
tb = Range(Cells(3, "L").MergeArea.Address).Width '单元宽度
tc = .Height '图片高度
td = .Width '图片宽度
tm = Application.WorksheetFunction.Min(ta / tc, tb / td) '单元与图片之间长宽差异比例的最小值
.Top = ActiveCell.Top + 2
.Left = ActiveCell.Left + 1
.Height = .Height * tm * 0.98 '按比例调整图片宽度
.Width = .Width * tm * 0.98 '按比例调整图片高度
End With
Cells(3, 4).Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
3、在当前目录下建个名为”照片“的子目录,里面存有以姓名为名称的.jpg格式的照片
4、在姓名后单元输入姓名后,就能自动插入图片了
全部回答
- 1楼网友:杯酒困英雄
- 2021-01-13 04:56
求知道楼主最后解决办法
- 2楼网友:野味小生
- 2021-01-13 04:30
EXCEL表格,A列是产品图片,B列是货号,能否在B列输入货号(如123)的同时把放在D:\tmp目录下的相应货号的JPG图片调出来,且大小与A列单元格完全相符.
用vba,按下ALT+F11,菜单:插入-模块,复制下面代码至代码框.按F5即可.
Sub addpicture()
Dim FirstRow, LastRow As Integer, FileType As String
FirstRow = Sheet1.UsedRange.Row
LastRow = FirstRow + Sheet1.UsedRange.Rows.Count - 1
FileType = InputBox("输入你的图片的后缀名", "输入图片格式", "jpg")
For i = FirstRow To LastRow
Numb = Cells(i, 2).Value
With ActiveSheet
.Pictures.Insert("D:\tmp\" & Numb & "." & FileType).Select
Set Target = .Cells(i, 1)
End With
With Selection
.Top = Target.Top + 1
.Left = Target.Left + 1
.Width = Target.Width - 1
.Height = Target.Height - 1
End With
Next i
End Sub
说明,图片文件夹 图片文件名与输入货号一致
看下这个好使不?
用vba,按下ALT+F11,菜单:插入-模块,复制下面代码至代码框.按F5即可.
Sub addpicture()
Dim FirstRow, LastRow As Integer, FileType As String
FirstRow = Sheet1.UsedRange.Row
LastRow = FirstRow + Sheet1.UsedRange.Rows.Count - 1
FileType = InputBox("输入你的图片的后缀名", "输入图片格式", "jpg")
For i = FirstRow To LastRow
Numb = Cells(i, 2).Value
With ActiveSheet
.Pictures.Insert("D:\tmp\" & Numb & "." & FileType).Select
Set Target = .Cells(i, 1)
End With
With Selection
.Top = Target.Top + 1
.Left = Target.Left + 1
.Width = Target.Width - 1
.Height = Target.Height - 1
End With
Next i
End Sub
说明,图片文件夹 图片文件名与输入货号一致
看下这个好使不?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息