nazeefmasood
New Member
- Joined
- Aug 20, 2023
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
Hi I am trying to save the data into the MainInventory but unable to do as I keep getting this error
When my sheet is empty and insert the second row as my first row is headers row the data is inserts fine everything works fine but as i enter the third row this pops ups and my vba closes and excels restart itself automatically
I also used different method to get the last row.
When my sheet is empty and insert the second row as my first row is headers row the data is inserts fine everything works fine but as i enter the third row this pops ups and my vba closes and excels restart itself automatically
VBA Code:
Private Sub cmdBtnSave_Click()
Dim addItemPage As Object
Dim mainWorksheet As Worksheet
Dim lastRow As Long
Set mainWorksheet = ThisWorkbook.Sheets("MainInventory")
Set addItemPage = frmInventorySystem.multipageInventory.Pages(0)
If Len(addItemPage.Controls("txtQtyAdd").Value) = 0 Then
MsgBox "Please enter a value for Quantity."
Exit Sub
End If
If Len(addItemPage.Controls("txtBoughtPerItem").Value) = 0 Then
MsgBox "Please enter a value for Bought Price."
Exit Sub
End If
If Len(addItemPage.Controls("txtExpenseAddItem").Value) = 0 Then
MsgBox "Please enter a value for Expense."
Exit Sub
End If
If Len(addItemPage.Controls("txtWholesalePricePeritem").Value) = 0 Then
MsgBox "Please enter a value for Wholesale Price."
Exit Sub
End If
Dim dealerName As String
Dim productName As String
Dim unitName As String
Dim productItemTotal As Double
Dim productGrandTotal As Double
Dim productQty As Double
Dim boughtAt As Double
Dim sellingPrice As Double
Dim wholeSalePrice As Double
Dim expense As Double
Dim perItemExpense As Double
Dim roudedValue As Double
lastRow = [CountA(MainInventory!A:A)] + 1
productName = addItemPage.Controls("comboProductNameAddItem").Value
unitName = addItemPage.Controls("comboUnitAddItem").Value
dealerName = addItemPage.Controls("comboBoxDealerName").Value
productQty = addItemPage.Controls("txtQtyAdd").Value
boughtAt = addItemPage.Controls("txtBoughtPerItem").Value
expense = addItemPage.Controls("txtExpenseAddItem").Value
sellingPrice = addItemPage.Controls("txtSellingPricePerItem").Value
wholeSalePrice = addItemPage.Controls("txtWholesalePricePeritem").Value
perItemExpense = expense / productQty
productItemTotal = boughtAt * productQty
productGrandTotal = (boughtAt * productQty) + expense
roudedValue = Application.WorksheetFunction.RoundUp(perItemExpense, 2)
With mainWorksheet
.Cells(lastRow, 1).Value = lastRow - 1
.Cells(lastRow, 2).Value = productName
.Cells(lastRow, 3).Value = dealerName
.Cells(lastRow, 4).Value = unitName
.Cells(lastRow, 5).Value = productQty
.Cells(lastRow, 6).Value = boughtAt
.Cells(lastRow, 7).Value = sellingPrice
.Cells(lastRow, 8).Value = wholeSalePrice
.Cells(lastRow, 9).Value = roudedValue
.Cells(lastRow, 10).Value = expense
.Cells(lastRow, 12).Value = productGrandTotal
.Cells(lastRow, 13).Value = Format(Now(), "dd-mm-yyyy hh:mm:ss")
End With
addItemPage.Controls("comboProductNameAddItem").Value = ""
addItemPage.Controls("comboUnitAddItem").Value = ""
addItemPage.Controls("comboBoxDealerName").Value = ""
addItemPage.Controls("txtQtyAdd").Value = ""
addItemPage.Controls("txtBoughtPerItem").Value = ""
addItemPage.Controls("txtExpenseAddItem").Value = ""
addItemPage.Controls("txtSellingPricePerItem").Value = ""
addItemPage.Controls("txtWholesalePricePeritem").Value = ""
MsgBox "Data saved successfully.", vbInformation, "Success"
End Sub
I also used different method to get the last row.
VBA Code:
lastRow = Application.WorksheetFunction.CountA(mainInventoryWorksheet.Columns("A")) + 1