具有用户窗体输入的Excel VBA仅可运行一次

时间:2019-12-23 12:25:37

标签: excel vba

基本上,我已经完成了此用户窗体和vba代码,以根据一个组合框值在工作簿中的不同工作表上输入数据。

一切都对一个条目有效,然后,如果我尝试添加另一笔付款,则会给我一个错误“运行时错误'1004”

我怀疑这是定义范围和选择的东西,我可能已经弄乱了。

Private Sub ComboBox_PaymentDay_Change()
Sheets(Me.ComboBox_PaymentDay.Value).Activate
paymenttable = "payment" & Me.ComboBox_PaymentDay.Value
Cells(1, 1).Select

End Sub

Private Sub CommandButton_AddPayment_Click()
    'Check for empty or False value for all checkbox and exit before submitting the form
Dim paymenttable As Variant
paymenttable = "payment" & Me.ComboBox_PaymentDay.Value
Dim currentsheet As Worksheet
Sheets(Me.ComboBox_PaymentDay.Value).Activate
With Payment_Form
If TextBox_ReceiptNo.Value = "" Then
        MsgBox "Receipt Number is required"
        Exit Sub
    ElseIf TextBox_Name.Value = "" Then
        MsgBox "Name is required"
        Exit Sub
    ElseIf OptionButton_Cheque.Value = True Then
        If TextBox_ChequeNo.Value = "" Then
            MsgBox "Cheque Number is Required"
            Exit Sub
        ElseIf ComboBox_Bank.ListIndex < 0 Then
            MsgBox "Please Select a Bank"
            Exit Sub
        End If

    ElseIf ComboBox_Currency.ListIndex < 0 Then
        MsgBox "Please select a Currency"
        Exit Sub
    ElseIf TextBox_Amount.Value = "" Then
        MsgBox "Amount is required"
        Exit Sub
    ElseIf OptionButton_Other.Value = True Then
        If TextBox_Code.Value = "" Then
            MsgBox "Account Code is Required"
            Exit Sub
        End If

End If
End With
    'Copy input values to sheet.
    Cells(1, 1).Select
    Dim oNewRow As ListRow

    Dim rng As Range
    Sheets(Me.ComboBox_PaymentDay.Value).Activate
    Set rng = ActiveWorkbook.Worksheets(Me.ComboBox_PaymentDay.Value).Range(paymenttable)
    rng.Select
    Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)

    Sheets(Me.ComboBox_PaymentDay.Value).Unprotect Password:="0000"
    With Sheets(Me.ComboBox_PaymentDay.Value)
        Debug.Print Payment_Form.TextBox_ReceiptNo.Value
        oNewRow.Range.Cells(1, 1).Value = Me.TextBox_ReceiptNo.Value
        oNewRow.Range.Cells(1, 2).Value = Me.TextBox_Name.Value
        oNewRow.Range.Cells(1, 3).Value = Me.TextBox_ChequeNo.Value
        oNewRow.Range.Cells(1, 4).Value = Me.DTPicker_ChequeDate.Value
        oNewRow.Range.Cells(1, 5).Value = Me.ComboBox_Bank.Value
        oNewRow.Range.Cells(1, 6).Value = Me.ComboBox_Currency.Value
        oNewRow.Range.Cells(1, 7).Value = Me.TextBox_Amount.Value
        If OptionButton_Cheque.Value = True Then
            oNewRow.Range.Cells(1, 10).Value = "Cheque"
        Else
            oNewRow.Range.Cells(1, 10).Value = "Cash"
            oNewRow.Range.Cells(1, 4).Value = "N/A"
            oNewRow.Range.Cells(1, 3).Value = "N/A"
            oNewRow.Range.Cells(1, 5).Value = "N/A"
        End If
        If OptionButton_Receivables.Value = True Then
            oNewRow.Range.Cells(1, 11).Value = "Receivables"
        Else
            oNewRow.Range.Cells(1, 11).Value = "Other"
        End If
        oNewRow.Range.Cells(1, 14).Value = Me.TextBox_Code.Value
        Me.TextBox_ReceiptNo.Value = ""
        Me.TextBox_Name.Value = ""
        Me.TextBox_ChequeNo.Value = ""
    'Me.DTPicker_ChequeDate.Value = ""
        Me.ComboBox_Bank.Value = ""
        Me.ComboBox_Currency.Value = ""
        Me.TextBox_Amount.Value = ""
        Set oNewRow = Nothing


    End With

    'Load Payment_Form
    Unload Me
    Sheets(Me.ComboBox_PaymentDay.Value).Protect Password:="0000"



End Sub






Private Sub CommandButton2_Click()

End Sub

Private Sub CommandButton_CloseForm_Click()
Unload Me
End Sub

Private Sub OptionButton_Cash_Click()
If OptionButton_Cash.Value = True Then
    Frame_ChequeData.Enabled = False
    Frame_ChequeData.Visible = False
    Else
    Frame_ChequeData.Enabled = True
    Frame_ChequeData.Visible = True
End If
End Sub

Private Sub OptionButton_Cheque_Click()
If OptionButton_Cheque.Value = True Then
    Frame_ChequeData.Enabled = True
    Frame_ChequeData.Visible = True
    Else
    Frame_ChequeData.Enabled = False
    Frame_ChequeData.Visible = False

End If
End Sub
Private Sub OptionButton_Receivables_Click()
If OptionButton_Receivables.Value = True Then
    Frame_Other.Enabled = False
    Frame_Other.Visible = False

    Else
    Frame_Other.Enabled = True
    Frame_Other.Visible = True

End If
End Sub
Private Sub OptionButton_Other_Click()
If OptionButton_Other.Value = True Then
    Frame_Other.Enabled = True
    Frame_Other.Visible = True
    Else
    Frame_Other.Enabled = False
    Frame_Other.Visible = False

End If
End Sub



Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
Me.ComboBox_PaymentDay.Value = Day(Date)
Dim BanksArray As Variant
Dim CurrencyArray As Variant

CurrencyArray = Array("EGP E£", "USD $", "EURO €")
BanksArray = Array("Abu Dhabi Islamic Bank (ADIB)", "Agricultural Bank of Egypt", "Ahli United Bank", "Al Ahli Bank of Kuwait - Egypt (ABK-Egypt)", "Al Baraka Bank of Egypt", "Arab African International Bank", "Arab Bank Plc.", "Arab Banking Corporation (Bank ABC)", "Arab International Bank", "Arab Investment Bank (AIBK)", "Attijariwafa Bank Egypt", "Bank Audi", "Bank of Alexandria", "Banque du Caire", "Banque Misr", "Blom Bank", "Citibank", "Commercial International Bank (CIB)", "Credit Agricole Egypt", "Egyptian Arab Land Bank", "Egyptian Gulf Bank (EG BANK)", "Emirates NBD", "Export Development Bank of Egypt", "Faisal Islamic Bank of Egypt", "First Abu Dhabi Bank (FAB)", "Housing and Development Bank", "HSBC Bank Egypt", "Industrial Development & Workers Bank of Egypt", "Misr Iran Development Bank", "National Bank of Egypt", "National Bank of Greece", _
"National Bank of Kuwait - Egypt (NBK-Egypt)", "Qatar National Bank Al Ahli (QNB Alahli)", "Société Arabe Internationale de Banque (SAIB)", "Suez Canal Bank", "The United Bank of Egypt", "Union National Bank Egypt (UNB-E)")
    Me.ComboBox_Bank.List = BanksArray
    Me.ComboBox_Currency.List = CurrencyArray
    Me.ComboBox_PaymentDay.List = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)
End Sub

Private Sub TextBox_ReceiptNo_Change()

    OnlyNumbers

End Sub


Private Sub TextBox_Amount_Change()

    OnlyNumbers

End Sub

Private Sub OnlyNumbers()



    If TypeName(Me.ActiveControl) = "TextBox" Then

        With Me.ActiveControl

            If Not IsNumeric(.Value) And .Value <> vbNullString Then

                MsgBox "Sorry, only numbers allowed"

                .Value = vbNullString

            End If

        End With

    End If
End Sub

我还有很多工作要做以清理代码并添加更多功能,但是我无法做更多的事情,因为我无法弄清楚。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

我通过替换解决了我的问题

Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)

使用

Set tbl = Range(paymenttable).ListObject

Set oNewRow = tbl.ListRows.Add(AlwaysInsert:=True)