【发布时间】:2019-08-29 14:27:59
【问题描述】:
我在尝试让 Excel 填写模板表单中的某些字段,然后将其复制/粘贴到另一个 WorkSheet 上时遇到了最奇怪的经历。然后它必须重复这个过程有限的次数,总是把表格粘贴在前面的表格下面。当页面已满时,它必须复制第 1 页的页眉并将其粘贴到下一页的顶部,然后进行表单复制/粘贴过程。
这个例程是我目前(尝试)开发的 VB6 小计算软件的一部分。
问题是:excel 无法复制我想要的数据并将其粘贴到第二个工作表中的正确位置。有时它根本不复制数据,当我将它导出为pdf时得到一张空白纸;有时它会复制所有内容,但会将填写的模板表单粘贴到错误的单元格中。
奇怪的行为:仅当 Excel 工作簿设置为不可见时才会发生。如果我暂停执行并将其设置为可见,则例程运行得很好,最后我会得到一个完美的 pdf 报告。
对发生这种情况的原因有什么想法和/或解决方法吗?
提前致谢。
我认为这可能是由于 VB6 exe 执行速度太快而无法处理 excel,因此我在代码行之间添加了几个“Sleep 1000”,希望 excel 能够赶上。没用。
With xlsm.Worksheets("Template Cabos")
.Activate
Sleep 1000
ActiveSheet.Range(Cells(1, 1), Cells(5, 36 + ONS)).Copy 'Copies the filled in form
Sleep 1000
End With
With xlsm.Worksheets("Mem. Cabos")
.Activate
Cells(lin, 1).Select
.Paste 'Pastes it in the report workbook
lin = lin + 6
If (lin + 6) > (pag * 33) Then 'Checks if the next form would fit into the page Checa se cabe outra ficha na página
lin = pag * 33 + 1 'If not, the marker "lin" goes to the top of page 2 Se não couber, o marcador "lin" vai pro topo da pág. 2
.Range("A1:AJ5").Copy 'Copies the header of page 1 Copia o cabeçalho da pág. 1
Cells(lin, 1).Select
.Paste 'Pastes on the top of page 2 Cola no topo da pág. 2
pag = pag + 1 'Acknowledges that now the report has one more page Sinaliza que agora o relatório tem 2 páginas
Cells(lin + 4, 35).Value = pag 'Types "2" in the field "page" of last page's header Escreve "2" no campo "Pág." do cabeçalho da pág. 2
lin = lin + 7 'Moves the marker 7 line down to receive the next form Move o marcador 7 linhas abaixo para receber a próx. ficha
End If
End With
【问题讨论】:
-
一般来说,你想在你的代码中avoid using activate and select
-
但如果不选择单元格,我将无法将内容粘贴到其中。 .Cells(lin,1).paste 出现错误
-
我刚刚意识到您在评论中给了我一个学习链接。这似乎非常有用。如果运气好的话,我会查一下,然后回来。谢谢。