|
- Sub insertPic()
- '
- '删除已有图片
- Dim S1 As Shape '声明变量 as后 变量类型
- 'Dim RG As Range
- For Each S1 In ActiveSheet.Shapes 'ActiveSheet 代表活动工作簿中的,或者指定的窗口或工作簿中的活动工作表(最上面的工作表)。
- If S1.Type <> 8 Then 'type属性返回图表类型
- S1.Delete
- End If
- Next S1
- '插入图片
- imgWidth = InputBox("请输入一个,大于等于1 数字", "设置插入图片列的宽度", 20)
- imgHeight = InputBox("请输入一个,大于等于1 数字", "设置插入图片行的高度", 40)
- hColumn = InputBox("请输入图片插入的列数", "设置插入图片行的高度(大于等于 1 的整数)", 11)
-
-
- If hColumn <> "" And imgWidth <> "" And imgHeight <> "" Then
- 'Fix 函数返回参数的整数部分
- If IsNumeric(hColumn) = True And IsNumeric(imgWidth) = True And IsNumeric(imgHeight) = True And hColumn - Fix(hColumn) = 0 And hColumn > 0 And imgWidth >= 1 And imgHeight >= 1 Then
- imgWidth = CDbl(imgWidth)
- imgHeight = CDbl(imgHeight)
- hColumn = CInt(hColumn)
- Dim i As Integer
- Dim FilPath As String
- Dim rng As Range
- Dim S As String
- S = ""
- With Sheet1
- For i = 2 To .Range("a65536").End(xlUp).Row '查找A列从65536位置的单元格起,向上查找,直到找到最后一个非空单元格为止,并显示其行号
- If Trim(Cells(i, 1).Text) <> "" Then
- FilPath = ThisWorkbook.Path & "\photos" & .Cells(i, 1).Text & ".jpg"
- 'FilPath = "D:\vba\vbatest\photos" & .Cells(i, 1).Text & ".jpg"
- If Dir(FilPath) <> "" Then
-
- .Pictures.Insert(FilPath).Select '选中
- Set rng = .Cells(i, hColumn)
- With Selection '当前的选择对象
- ActiveSheet.Rows(i).RowHeight = imgHeight '调整行高适合图片大小 Selection.ShapeRange.Height * imgHeight
- 'MsgBox ActiveSheet.Rows(i).RowHeight
- ActiveSheet.Columns(hColumn).ColumnWidth = imgWidth '粗略调整列宽适合图片大小 Selection.ShapeRange.Width * imgWidth
- .Top = rng.Top + 1
- .Left = rng.Left + 2
- .Width = rng.Width
- .Height = rng.Height
- End With
- Else
- S = S & Chr(10) & .Cells(i, 1).Text
- End If
- End If
- Next
- .Cells(hColumn, i).Select
- End With
- If S <> "" Then
- MsgBox S & Chr(10) & "没有照片"
- End If
- Else
- MsgBox "输入有误"
- End If
- End If
- End Sub
复制代码 现在是想在打开 execl 后直接在里面输入参数,而不需要点击 “插入图片” 根据 inputbox 的输入参数来执行?
不知道没有这样的效果
修改以下的几句代码,把=后的内容改为你存放参数的单元格即可,譬如imgWidth=range("A1")
imgWidth = InputBox("请输入一个,大于等于1 数字", "设置插入图片列的宽度", 20)
imgHeight = InputBox("请输入一个,大于等于1 数字", "设置插入图片行的高度", 40)
hColumn = InputBox("请输入图片插入的列数", "设置插入图片行的高度(大于等于 1 的整数)", 11)
|
-
效果图
|