我正在尝试在Excel中创建一个Vb脚本,它将需要6个文本框输入并将它们放在所需的位置(我有完美的工作)接下来我需要它来创建“X”份数并增加一个特定的Cell 。我有这个工作到一定程度。我得到两张有“1张X”的纸张,剩下的复印纸张都很好......“2张X”......“3张X”等等。当我尝试打印或删除所有表格(Sheet1除外)时,当我有大约10张纸张创建Excel冻结时。对我做错了什么的想法? 感谢您的任何意见。
Private Sub CommandButton1_Click()
' sets my text from the text boxes to the excel cells
Range("C3").Value = TextBox2.Text
Range("C4").Value = TextBox3.Text
Range("C5").Value = TextBox4.Text
Range("C6").Value = TextBox5.Text
Range("E11").Value = TextBox6.Text
Range("I15").Value = TextBox1.Text
Dim p As Integer
' this is my value that gets incremented so its started at 1
Range("G15").Value = "1"
' this is my loop that creates each sheet and increments the X of X number
For p = 0 To (TextBox1.Text - 1)
Sheet1.Copy After:=Sheet1
Range("G15").Value = (p + 1)
Range("C3").Value = TextBox2.Text
Range("C4").Value = TextBox3.Text
Range("C5").Value = TextBox4.Text
Range("C6").Value = TextBox5.Text
Range("E11").Value = TextBox6.Text
Range("I15").Value = TextBox1.Text
Next p
End Sub
Private Sub CommandButton2_Click()
'This deletes all my extra sheets and clears my text boxes
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
If ws.Name <> "Sheet1" Then ws.Delete
Next
Application.DisplayAlerts = True
Range("C3").Value = ""
Range("C4").Value = ""
Range("C5").Value = ""
Range("C6").Value = ""
Range("E11").Value = ""
Range("I15").Value = ""
Range("G15").Value = ""
End Sub
Private Sub CommandButton3_Click()
'My button on the excel sheet to open the user form
UserForm1.Hide
End Sub
Private Sub CommandButton4_Click()
'This is my print out the sheets button
Dim ws As Worksheet
Dim i As Integer
i = 0
For Each ws In ActiveWorkbook.Worksheets
If (i = 0) Then
ws.Select
Else
ws.Select False
End If
i = i + 1
Next ws
Application.Dialogs(xlDialogPrinterSetup).Show
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End Sub
更新(可能有助于获取错误信息) 我可以确认它发生在TextBox1.Text
的9以上错误说.....
运行时错误'-2147417848(80010108)':
自动化错误 调用的对象已与其客户端断开连接
答案 0 :(得分:0)
更改您的
Sheet1.Copy After:=Sheet1
到
Sheet1.Copy Before:=Sheet1
该错误可能与应用程序因运行宏而冻结有关。尝试在您的操作正在运行时禁用屏幕更新。
Application.ScreenUpdating = False
Application.ScreenUpdating = True