|
- Sub main()
- Dim source_arr, temp_arr, ws_name_arr
- Dim cnt%, i%, j%, k%, ws_cnt%, end_row%
- Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
-
- Set ws1 = Worksheets("基础数据源")
- Set ws2 = Worksheets("加工合同模板")
-
-
- ' 读取数据源
- end_row = ws1.Range("P66356").End(xlUp).Row
- source_arr = ws1.Range("A5:P" & end_row)
-
- ' 在合同模板中写入基础信息
- For i = 1 To UBound(source_arr)
- ' 将合同另存为新sheet,并改名为受托方
- ws2.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
- Set ws3 = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
-
- ' 统计内容的行数
- cnt = 0
- For j = i To UBound(source_arr)
- cnt = cnt + 1
- If j = end_row - 4 Then
- Exit For
- Else
- If Len(source_arr(i + cnt, 2)) Then
- Exit For
- End If
- End If
- Next j
-
- With ws3
- .Cells(2, "L") = source_arr(i, 2) ' 合同号
- .Cells(6, "B") = source_arr(i, 5) ' 受托方
- .Cells(7, "B") = source_arr(i, 3) ' 地址
- .Cells(8, "B") = source_arr(i, 4) ' 电话
-
- ' 判断合同模板的数据行数是否足够
- If cnt > 20 Then
- For j = 1 To cnt - 20
- .Rows(32).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
- Next j
- End If
-
- ' 将该合同号下的数据写入temp_arr
- ReDim temp_arr(1 To cnt, 1 To 11)
- k = 1
- Do While k <= cnt
- For j = 1 To 11
- temp_arr(k, j) = source_arr(i, j + 5)
- Next j
- i = i + 1
- k = k + 1
- Loop
- ' 将temp_arr写入合同的数据区域
- .Cells(13, "A").Resize(UBound(temp_arr, 1), UBound(temp_arr, 2)) = temp_arr
-
- ' 修改sheet名
- ws_cnt = ws_cnt + 1
- ws3.Name = ws_cnt & "_" & ws3.Cells(6, "B")
- End With
-
- i = i - 1
-
- Next i
-
-
- End Sub
复制代码
|
|