2014-07-10 97 views
0

我想在Excel中創建一個Vb腳本,將需要6文本框輸入並將它們放在所需的位置(我有那完美的工作)接下來我需要它來創建「X」份數並增加特定單元格。我有這個工作的重點。我得到兩張帶有「X的1」的紙張,其餘的複印紙很好......「X的2個」......「X的3個」等等。當我有更多的時候,當我嘗試打印或刪除所有表格(Sheet1除外)時創建Excel凍結10張左右。任何想法,我在做什麼錯? 感謝您的任何輸入。Excel vb腳本崩潰當試圖打印或清除表

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 

更新(可幫助有錯誤信息) 我可以證實它發生於任何超過9爲TextBox1.Text

錯誤說.....

運行時間錯誤 '-2147417848(80010108)':

自動化錯誤 調用已經與其客戶端斷開連接的對象

回答

0

更改

Sheet1.Copy After:=Sheet1 

Sheet1.Copy Before:=Sheet1 

錯誤可能涉及到的應用程序運行的宏觀凍結。嘗試在您的操作正在運行時禁用屏幕更新。

Application.ScreenUpdating = False 

Application.ScreenUpdating = True 

source