Runtime error -2147417848 (80010108) method value of the object range failed

nazeefmasood

New Member
Joined
Aug 20, 2023
Messages
1
Office Version
  1. 365
Platform
  1. 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
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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top