|
修改后的代码,已测试过了,你再试试
Sub 批量导出到模板3()
Dim ar, br, cr, i%, j%, wb1 As Workbook, wb2 As Workbook, wb As Workbook, d As Object
Set wb1 = ThisWorkbook
ar = wb1.Sheets("数据").Range("a2:il" & Cells(Rows.Count, 1).End(xlUp).Row) '这句有修改,请与你原来的对照
ReDim br(1 To UBound(ar) + 1, 1 To UBound(ar, 2))
Set d = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(ar)
For j = 1 To UBound(ar, 2)
br(1, j) = Replace(Cells(1, j).Address(False, False), "1", "") & "列"
If ar(i, j) = "" Then
Else
br(i + 1, j) = ar(i, j)
End If
Next j
Next i
Set wb2 = Workbooks.Open(wb1.Path & "\模板1.xls")
For m = 1 To UBound(ar)
For j = 2 To UBound(br, 2)
d(br(m + 1, 1) & "-" & br(1, j)) = br(m + 1, j)
Next j
Set wb = Workbooks.Add
wb2.Sheets.Copy before:=wb.Sheets(1)
With wb.Sheets("工作表1")
cr = .Range("a3:k45")
cr = .Range("a3:gi27") '这里写错了,已修改成上一句
For i = 1 To UBound(cr)
For j = 1 To UBound(cr, 2)
If Right(cr(i, j), 1) = "列" Then
.Cells(i + 2, j) = d(ar(m, 1) & "-" & cr(i, j))
Else
Select Case cr(i, j) '这里用“select case"不合适,也可以运行就不改了
Case "箱 内 设 备 清 单"
For s = 1 To 21
.Cells(i + s + 2, j + 2) = ar(m, 5 * (s - 1) + 17)
.Cells(i + s + 2, j + 3) = ar(m, 5 * (s - 1) + 18)
.Cells(i + s + 2, j + 4) = ar(m, 5 * (s - 1) + 19)
.Cells(i + s + 2, j + 5) = ar(m, 5 * (s - 1) + 20)
.Cells(i + s + 2, j + 6) = ar(m, 5 * (s - 1) + 21)
Next s
End Select
End If
Next j
Next i
.Cells(10, 20).Value = ar(m, 1) '
End With
wb.SaveAs (wb1.Path & "\" & ar(m, 2) & ".xls")
wb.Close
d.RemoveAll
Next m
MsgBox "导出完毕!"
End Sub |
|