|
楼主 |
发表于 2016-2-18 12:29
|
显示全部楼层
看赵老师解答后,借鉴方法,简化如下:
- Sub 批量打印()
- Dim sVoucher, sProject, i, ADOConn, ADORst
- Set ADOConn = CreateObject("ADODB.Connection")
- Set ADORst = CreateObject("ADODB.Recordset")
- conn = "provider=microsoft.ACE.OLEDB.12.0;extended properties='Excel 12.0;hdr=yes';data source=" & ThisWorkbook.FullName
- Rmax = Sheets("查询清单").Range("a1").CurrentRegion.Rows.Count
- For i = 2 To Rmax
- sVoucher = Sheets("查询清单").Cells(i, 1)
- sProject = Sheets("查询清单").Cells(i, 2)
- SQL = "select 名称,规格,单位,数量,单价,金额 from [项目名称$] where [凭证号]=""" & sVoucher & """ And [项目名称]= """ & sProject & """"
- ADOConn.Open conn
- ADORst.Open SQL, ADOConn, 3, 3
- Debug.Print ADORst.RecordCount
- With Sheets("打印模板")
- Union(.[B2], .[C2], .[G2]).ClearContents
- .[B2] = sProject
- .[e2] = Split(sVoucher, "-")(2)
- .[G2] = Left(sVoucher, InStrRev(sVoucher, "-")) & Int(Rnd * 3 + 25)
- While Not ADORst.EOF
- .Range(.Cells(4, 2), .Cells(13, 7)).ClearContents
- .Cells(4, 2).CopyFromRecordset ADORst, 10
- ActiveSheet.PrintOut
- Wend
- End With
- ADORst.Close
- ADOConn.Close
- Next i
- Set ADORst = Nothing
- Set ADOConn = Nothing
- End Sub
复制代码
|
|