Sub
InsertPic()
Dim
arr, i&, k&, n&, b
As
Boolean
Dim
strPicName$, strPicPath$, strFdPath$, shp
As
Shape
Dim
rngData
As
Range, rngEach
As
Range, rngWhere
As
Range, strWhere
As
String
With
Application.FileDialog(msoFileDialogFolderPicker)
If
.Show
Then
strFdPath = .SelectedItems(1)
Else
:
Exit
Sub
End
With
If
Right(strFdPath, 1) <>
"\" Then strFdPath = strFdPath & "
\"
Set
rngData = Application.InputBox(
"请选择图片名称所在的单元格区域"
, Type:=8)
Set
rngData = Intersect(rngData.Parent.UsedRange, rngData)
If
rngData
Is
Nothing
Then
MsgBox
"选择的单元格范围不存在数据!"
:
Exit
Sub
strWhere = InputBox(
"请输入图片偏移的位置,例如上1、下1、左1、右1"
, ,
"右1"
)
If
Len(strWhere) = 0
Then
Exit
Sub
x = Left(strWhere, 1)
If
InStr(
"上下左右"
, x) = 0
Then
MsgBox
"你未输入偏移方位。"
:
Exit
Sub
y = Val(Mid(strWhere, 2))
Select
Case
x
Case
"上"
Set
rngWhere = rngData.Offset(-y, 0)
Case
"下"
Set
rngWhere = rngData.Offset(y, 0)
Case
"左"
Set
rngWhere = rngData.Offset(0, -y)
Case
"右"
Set
rngWhere = rngData.Offset(0, y)
End
Select
Application.ScreenUpdating =
False
rngData.Parent.Parent.Activate
rngData.Parent.
Select
For
Each
shp
In
ActiveSheet.Shapes
If
Not
Intersect(rngWhere, shp.TopLeftCell)
Is
Nothing
Then
shp.Delete
Next
x = rngWhere.Row - rngData.Row
y = rngWhere.Column - rngData.Column
arr = Array(
".jpg"
,
".jpeg"
,
".bmp"
,
".png"
,
".gif"
)
For
Each
rngEach
In
rngData
strPicName = rngEach.Text
If
Len(strPicName)
Then
strPicPath = strFdPath & strPicName
b =
False
For
i = 0
To
UBound(arr)
If
Len(Dir(strPicPath & arr(i)))
Then
Set
shp = ActiveSheet.Shapes.AddPicture( _
strPicPath & arr(i),
False
,
True
, _
rngEach.Offset(x, y).Left + 5, _
rngEach.Offset(x, y).Top + 5, _
20, 20)
shp.
Select
With
Selection
.ShapeRange.LockAspectRatio = msoFalse
.Height = rngEach.Offset(x, y).Height - 10
.Width = rngEach.Offset(x, y).Width - 10
End
With
b =
True
n = n + 1
Range(
"a1"
).
Select
:
Exit
For
End
If
Next
If
b =
False
Then
k = k + 1
End
If
Next
Application.ScreenUpdating =
True
MsgBox
"共处理成功"
& n &
"个图片,另有"
& k &
"个非空单元格未找到对应的图片。"
End
Sub