|
- Sub 生成报告()
- Dim arr, i%, j%, s%
- Dim sh As Worksheet, nsh As Worksheet
- Dim wb As Workbook, wbname As String
- Application.ScreenUpdating = False
- arr = Sheets("清单").UsedRange
- For i = 2 To UBound(arr)
- If arr(i, 21) = "" Then
- If Len(arr(i, 1)) > 0 Then
- For j = 12 To 16 Step 2
- wbname = arr(1, j) & " " & arr(i, 2) & " " & arr(i, 3) & " " & arr(i, 4) & " " & arr(i, 7)
- If Dir(ThisWorkbook.Path & "" & wbname & ".xls") <> "" Then
- MsgBox "您已生成过报告单", vbOKOnly, "ExcelHOME"
- MsgBox "即将退出程序,请检查ok后再运行", vbOKOnly, "ExcelHOME"
- Exit Sub
- End If
- If Len(arr(i, j)) > 0 Then
- Set sh = Sheets(arr(1, j))
- With sh
- If .Name = "金相" Then
- For s = 4 To 6
- .Cells(s, 2) = "" '清除原模板数据
- .Cells(s, 6) = ""
- Next s
- ElseIf .Name = "冲击" Then
- For s = 4 To 6
- .Cells(s, 3) = "" '清除原模板数据
- .Cells(s, 10) = ""
- Next s
- ElseIf .Name = "硬度" Then
- For s = 4 To 6
- .Cells(s, 3) = "" '清除原模板数据
- .Cells(s, 9) = ""
- Next s
- End If
- End With
- Sheets(sh.Name).Copy after:=Sheets(Sheets.Count)
- Set nsh = Sheets(Sheets.Count)
- With nsh
- If sh.Name = "金相" Then
- .Cells(4, 2) = arr(i, 6) '"规格"
- .Cells(5, 2) = arr(i, 5) '"炉号"
- .Cells(6, 2) = arr(i, 7) '"材质"
- .Cells(4, 6) = arr(i, 3) '"样品编号"
- .Cells(5, 6) = arr(i, 4) '"批号"
- .Cells(6, 6) = arr(i, 2) '"送检单位"
- ElseIf sh.Name = "冲击" Then
- .Cells(4, 3) = arr(i, 4) '"批号"
- .Cells(5, 3) = arr(i, 5) '"炉号"
- .Cells(6, 3) = arr(i, 6) '"规格"
- .Cells(4, 10) = arr(i, 7) '"材质"
- .Cells(5, 10) = arr(i, 2) '"送检单位"
- .Cells(6, 10) = arr(i, 3) '"样品编号"
- ElseIf sh.Name = "硬度" Then
- .Cells(4, 3) = arr(i, 4) '"批号"
- .Cells(5, 3) = arr(i, 5) '"炉号"
- .Cells(6, 3) = arr(i, 6) '"规格"
- .Cells(4, 9) = arr(i, 7) '"材质"
- .Cells(5, 9) = arr(i, 2) '"送检单位"
- .Cells(6, 9) = arr(i, 3) '"样品编号"
- End If
- Set wb = Workbooks.Add
- .Copy after:=wb.Sheets(1)
- wb.SaveAs ThisWorkbook.Path & "" & wbname & ".xls"
- wb.Close
- Application.DisplayAlerts = False
- .Delete
- Application.DisplayAlerts = True
- End With
- End If
- Next j
- Sheets("清单").Cells(i, 21) = "已生成"
- End If
- End If
- Next i
- MsgBox "生成报告单工作完成", vbOKOnly, "ExcelHOME"
- Worksheets("清单").Activate
- Application.ScreenUpdating = False
- End Sub
复制代码 |
|