VBA excel打印到用户forms的打印机

我有一个用户表单,填写时,底部有一个命令button。 当button被点击时,它将数据复制到工作表2,然后将行信息复制到工作表4中,这是一个窗体,closures用户窗体并打开欢迎用户窗体,然后我希望它打印工作表4。

这是我的代码。 打印function将不起作用。

Private Sub SavePrintButton_Click() EditAdd Sheet4.Activate Dim myValue As Variant myValue = TextBox1.Value Range("b2").Value = myValue Unload Me Function PrintOneSheet() Sheets("Sheet4").PrintOut End Function WelcomeUserForm.Show End Sub 

您的“PrintOneSheet”functionembedded在SavePrintButton_Click中。 在“卸载我”之后移动“结束子”并在“卸载我”上方插入“调用PrintOneSheet”。 我冒昧地添加了一个“Me.Hide”行,因为在显示WelcomeUserForm之前,您显然希望当前表单消失。 一旦不需要,它仍然会被卸载。 同时,如果因为某种原因想要回头去纠正某些问题,可以通过简单地再次显示来完成。

 Private Sub SavePrintButton_Click() Dim myValue As Variant EditAdd Sheet4.Activate myValue = TextBox1.Value Sheet4.Range("b2").Value = myValue Call PrintOneSheet Me.Hide WelcomeUserForm.Show Unload Me End Sub Function PrintOneSheet() Sheets("Sheet4").PrintOut End Function 

另一方面,Sheet4总是被称为“Sheet4”? Sheet4是直接调用对象ID,“Sheet4”是对其属性之一(它的名字)的调用。 你应该在你的引用中保持一致,否则总有一天你会改变一些显然很小的东西,混乱就会松散。

我已经重新编写了一些代码。 这看起来更像吗? 我摆脱了所有unload.me在我的vba。

 Private Sub SavePrintButton_Click() EditAdd Sheet6.Activate Dim myValue As Variant myValue = TextBox1.Value Range("b2").Value = myValue ClearForm Me.Hide Sheet6.PrintOut WelcomeUserForm.Show MsgBox "New Run Added" MsgBox "Run Sent to Printer" End Sub