Originally Posted by
SamT
Two things:
In your code,
lr is always referring to the last sheet it references, in your case, Sa, Savings
CountA is the wrong method, it is not guaranteed to be the actual last used Row.
Since you are using the last used Row value on many sheets, you will either need to use many different LastRow Variables, or recompute it for each sheet
A preferred method is
NextRow = Sheet.Cells(Rows.Count, "Column Letter").End(xlUp).Row + 1
'or
NextRow =CH.Cells(Rows.Count, "A").End(xlUp).Row + 1
'or
NextRow =CH.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
Hey SamT,
Thank you for the solution, the following includes the corrections I made:
Dim ChNr As Long
Dim CCNr As Long
Dim ExNr As Long
Dim LiNr As Long
Dim SaNr As Long
' Checking for Duplicates
' Checking
If Application.WorksheetFunction.CountIf(Ch.Range("A:A"), Me.txbEnterBudgetItemName.Value) > 0 Then
MsgBox "This Budget Item Name already exists, please enter a new name.", vbOKOnly
Exit Sub
End If
ChNr = Sheet7.Cells(Rows.Count, "A").End(xlUp).Row + 1
'Credit Card
If Application.WorksheetFunction.CountIf(CC.Range("A:A"), Me.txbEnterBudgetItemName.Value) > 0 Then
MsgBox "This Budget Item Name already exists, please enter a new name.", vbOKOnly
Exit Sub
End If
CCNr = Sheet9.Cells(Rows.Count, "A").End(xlUp).Row + 1
' Expense
If Application.WorksheetFunction.CountIf(Ex.Range("A:A"), Me.txbEnterBudgetItemName.Value) > 0 Then
MsgBox "This Budget Item Name already exists, please enter a new name.", vbOKOnly
Exit Sub
End If
ExNr = Sheet5.Cells(Rows.Count, "A").End(xlUp).Row + 1
' Liabilities
If Application.WorksheetFunction.CountIf(Li.Range("A:A"), Me.txbEnterBudgetItemName.Value) > 0 Then
MsgBox "This Budget Item Name already exists, please enter a new name.", vbOKOnly
Exit Sub
End If
LiNr = Sheet6.Cells(Rows.Count, "A").End(xlUp).Row + 1
' Savings
If Application.WorksheetFunction.CountIf(Sa.Range("A:A"), Me.txbEnterBudgetItemName.Value) > 0 Then
MsgBox "This Budget Item Name already exists, please enter a new name.", vbOKOnly
Exit Sub
End If
SaNr = Sheet8.Cells(Rows.Count, "A").End(xlUp).Row + 1
' Add Budget Item
If Me.cmbCategory.Value = "Checking" Then
Ch.Range("A" & ChNr).Value = Me.txbEnterBudgetItemName.Value
Ch.Range("B" & ChNr).Value = Me.txbBudgetBeginBal.Value
ElseIf Me.cmbCategory.Value = "Credit Card" Then
CC.Range("A" & CCNr).Value = Me.txbEnterBudgetItemName.Value
CC.Range("B" & CCNr).Value = Me.txbBudgetBeginBal.Value
ElseIf Me.cmbCategory.Value = "Expense" Then
Ex.Range("A" & ExNr).Value = Me.txbEnterBudgetItemName.Value
Ex.Range("B" & ExNr).Value = Me.txbBudgetBeginBal.Value
Ex.Range("C" & ExNr).Value = Me.txbBudgetTotalDue.Value
Ex.Range("D" & ExNr).Value = Me.cmbTermCode.Value
Ex.Range("E" & ExNr).Value = Me.cbxAutomaticWithdrawal.Value
ElseIf Me.cmbCategory.Value = "Liability" Then
Li.Range("A" & LiNr).Value = Me.txbEnterBudgetItemName.Value
Li.Range("B" & LiNr).Value = Me.txbBudgetBeginBal.Value
Li.Range("C" & LiNr).Value = Me.txbBudgetTotalDue.Value
Li.Range("D" & LiNr).Value = Me.cmbTermCode.Value
Li.Range("E" & LiNr).Value = Me.cbxAutomaticWithdrawal.Value
Else
Me.cmbCategory.Value = "Saving"
Sa.Range("A" & SaNr).Value = Me.txbEnterBudgetItemName.Value
Sa.Range("B" & SaNr).Value = Me.txbBudgetBeginBal.Value
End If
I wanted to use the Enum method you suggested, but I am afraid that my knowledge of VBA is still too basic as I wrote the original code within the form itself, which gave me an "Invalid Inside Procedure" error. Does this mean that I have to write it into a module? And if so, do I still write it as a sub procedure?