标签:VBA
如下图1所示,在工作表Sheet1中有一系列人员信息数据,包括人员照片、姓名、年龄,等。
图1
现在,要根据这些人员信息来给每个人制作信息卡片,模板如下图2所示。
图2
可以使用VBA来自动完成。
打开VBE,插入一个标准模块,在其中输入下面的代码:
Sub test()
Dim sh As Shape
Dim anchor As Range
Dim PicCell As Range
Dim nm As String
Dim age As Integer
Dim nSh As Shape
For Each sh In Sheet1.Shapes
Set anchor = sh.TopLeftCell
nm = anchor.Offset(, 1).Value
age = anchor.Offset(, 2).Value
Set PicCell = Sheet2.Cells.Find("Name", , , xlWhole).Offset(3).MergeArea
PicCell.Offset(-3) = nm
PicCell.Offset(3) = age
sh.Copy
Sheet2.Paste PicCell
Next sh
For Each sh In Sheet2.Shapes
sh.Height = sh.TopLeftCell.MergeArea.Height
sh.Width = sh.TopLeftCell.MergeArea.Width
Next sh
End Sub
运行代码,其效果如下图3所示。
图3
注:本示例整理自vbaexpress.com论坛,供有兴趣的朋友研究参考。