2015-11-02 49 views
-1

在frmClothingPricer中,当按下cmdPrint时,frmPrint会激活并打印,但会多次请求。我不希望10+ frmPrint“活跃”。如何在每个打印循环后关闭frmPrint?我在frmPrint上试过了“卸载我”,但是也没有卸载它。 ??我错过了什么?在Excel中打印后卸载表格

日常打印

If Len(HowMany) = 0 Then 
     End 
     Else 
      Do Until i = HowMany 
       frmPrint.Show 'prints form on activation 
       i = i + 1 
      Unload frmPrint 'this isn't working = several forms are open 
     Loop 
    End If 

frmPrint代码

Private Sub UserForm_Initialize() 

    PrintMe 

End Sub 

Private Sub PrintMe() 

    lblPrintMonthCode.Caption = frmClothingPricer.MonthCode 
    lblPrintPricer.Caption = frmClothingPricer.Pricer 
    lblPrintCost.Caption = (frmClothingPricer.Cost * 100) 
    lblPrintDescription.Caption = frmClothingPricer.Description 
    lblPrintPrice.Caption = frmClothingPricer.Price 
    lblPrintItemNumber = frmClothingPricer.ItemNumber 
    frmPrint.PrintForm 
     'tried unload.me here with same results 

End Sub 
+0

不要使用[类名来引用形式(http://stackoverflow.com/a/6049062/11683),明确创建实例。 – GSerg

+0

为什么要打开表单来打印然后关闭表单? – Davesexcel

+0

它使微小的标签哈哈,发现它很难打印出我想要的格式。 – instanceoftime

回答

0

我通过保持所有的代码,除了原来的形式在标签上解决了这个问题。最新的错误是围绕着变换的变量进行的。现在的作品完美(下图):

Form1中

Public Price As Double 
Public Percent As Double 
Public Cost As Currency 
Public Description As String 
Public MonthCode As Integer 
Public Pricer As String 
Public ItemNumber As Double 

Private Sub UserForm_Initialize() 
    Pricer = InputBox("Enter Your Pricer Number", vbOKOnly, "") 
    If Len(Pricer) = 0 Then 'Checking if Length of name is 0 characters 
     End 
    Else 
    End If 
End Sub 

Private Sub cmdSearch_Click() 
    Dim Response As Long 
    Dim NotFound As Integer 
    Dim arr As Variant 
    Dim i As Long 
    Dim str1 As String, str2 As String, str3 As String 

    lbxCost.BackColor = &H80000005 
    lbxCost.Locked = False 

    NotFound = 0 

    ActiveWorkbook.Sheets("Items").Activate 

    Response = Val("0" & Replace(txtItemNumber.Text, "-", "")) 

    ItemNumber = Response 

    If Response <> False Then 
     With ActiveSheet 
      arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row) 
     End With 

     For i = 1 To UBound(arr) 
      If arr(i, 1) = Response Then 
       str1 = IIf(str1 = "", arr(i, 2), str1 & "|" & arr(i, 2)) 
       str2 = IIf(str2 = "", arr(i, 3), str2 & "|" & arr(i, 3)) 
       str3 = IIf(str3 = "", arr(i, 4), str3 & "|" & arr(i, 4)) 
      End If 
     Next 

     If str1 = "" Then 
      MsgBox "Item Number Not Found!", vbExclamation 
      NotFound = 1 
      txtItemNumber.Text = "" 
      txtItemNumber.SetFocus 
     Else 
      Frame1.Visible = True 
      lbxDescription.List = Split(str1, "|") 
      lbxCost.List = Split(str2, "|") 
      ListBox3.List = Split(str3, "|") 
     End If 

    End If 

    lbxCost.ListIndex = 0 
End Sub 

Private Sub lbxCost_Click() 
    Frame2.Visible = True 
End Sub 

Private Sub lbxPercent_Click() 
    Frame3.Visible = True 

    lbxCost.BackColor = &H80000004 
    lbxCost.Locked = True 

    For x = 0 To lbxCost.ListCount - 1 
     If lbxCost.Selected(x) = True Then 
      Cost = lbxCost.List(x) 
      Description = lbxDescription.List(x) 
     End If 
    Next x 

    For y = 0 To lbxPercent.ListCount - 1 
     If lbxPercent.Selected(y) = True Then 
      Percent = lbxPercent.List(y) 
     End If 
    Next y 

    lblPrice.Caption = (Round(Cost * (1 + (Percent/100)), 0)) - 0.01 
    Price = lblPrice.Caption 
    lblItemNumber.Caption = txtItemNumber.Text 
    lblDescription.Caption = Description 
    MonthCode = (Year(Now)) + (Month(Now)) - 1765 
    lblMonthCode.Caption = MonthCode 
    lblPricer.Caption = Pricer 
    cmdPrint.SetFocus 
End Sub 

Private Sub cmdPrint_Click() 
    Dim i As Integer 
    Dim Howmany As Double 

    Load frmPopup 

    Howmany = Val(txtQuantity.Text) 

    i = 1 
    Do Until i > Howmany 
     frmPopup.PrintForm 
     i = i + 1 
    Loop 

    lbxPercent.ListIndex = -1 

    Frame1.Visible = False 
    Frame2.Visible = False 
    Frame3.Visible = False 
    txtItemNumber.Text = "" 

    txtItemNumber.SetFocus 

    Unload frmPopup 
End Sub 

窗口2

Private Sub UserForm_Initialize()   
    lblPrintMonthCode.Caption = frmClothingPricer.MonthCode 
    lblPrintPricer.Caption = frmClothingPricer.Pricer 
    lblPrintCost.Caption = (frmClothingPricer.Cost * 100) 
    lblPrintDescription.Caption = frmClothingPricer.Description 
    lblPrintPrice.Caption = frmClothingPricer.Price 
    lblPrintItemNumber = frmClothingPricer.ItemNumber 
End Sub