在Excel中打印后卸载表单

时间:2015-11-02 21:50:36

标签: excel-vba vba excel

在frmClothingPricer中,当按下cmdPrint时,frmPrint会激活并打印,但要求多次打印。我不想要10+ frmPrint"活跃"。如何在每次打印循环后关闭frmPrint?我在frmPrint"卸载我"但这并没有卸载它。 ??我错过了什么?

打印例程

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

1 个答案:

答案 0 :(得分: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