|
本帖最后由 MR-W 于 2018-9-26 14:52 编辑
Sub Export()
Dim Arr, Rule, N&, I&, FN$
With ThisWorkbook.Worksheets("数据源")
I = .Cells(.Rows.Count, 1).End(3).Row
If I > 1 Then
Arr = .Range("a2:i" & I).Value
Else
MsgBox "未找到有效数据!"
Exit Sub
End If
End With
Rule = Split("b2,e2,g2,b3,e3,g3,b4,e4,g4", ",")
For N = LBound(Arr) To UBound(Arr)
ThisWorkbook.Worksheets("模板").Copy
With ActiveWorkbook
With .ActiveSheet
.Name = Arr(N, 3)
For I = LBound(Rule) To UBound(Rule)
If I + 1 <= UBound(Arr, 2) Then
.Range(Rule(I)).Value = Arr(N, I + 1)
End If
Next I
For I = 6 To .Cells(.Rows.Count, 5).End(3).Row
If .Cells(I, 5).Value <> "" Then
FN = ThisWorkbook.Path & "\" & Arr(N, 3) & "\" & .Cells(I, 5).Value & ".jpg"
If Dir(FN) <> "" Then InsertPic .Cells(I, 5), FN
End If
Next I
End With
.SaveAs ThisWorkbook.Path & "\" & Arr(N, 3), 51
.Close False
End With
Next N
End Sub
Function InsertPic(ByRef Rng As Range, ByVal mPath As String) As String
Rng.Parent.Shapes.AddPicture mPath, True, True, Rng.Left + 1, Rng.Top + 1, Rng.Width - 2, Rng.Height - 2
End Function
|
|