Runtime Error 91 Object variable or with block variable not set

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
All I am wanting to do is if the item information is not found in the spreadsheet add it to the next available line but if it is found, locate the address and add the missing items by offsetting it from the location address. The error occurs on the code below and I have included all the code that has to do with rngItem. I know that if it doesn't find the item then there won't be any address assigned to it.
Code:
Set rngItem = Range(rngItem.Address)
Code:
Option Explicit


Public lDz As Long, lCs As Long, ws_Count As Long, i As Long
Public sUOM As String, sPrdCde As String, formTitle As String
Public rngItem As Range


Sub Test()


Dim finalRow As Long, X As Long
Dim Rng As Range


lDz = 0
lCs = 0
sUOM = ""


With ActiveWorkbook.Worksheets(formTitle)
    Set Rng = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
End With


Set rngItem = findItem(Rng, sPrdCde)


    If Not rngItem Is Nothing Then
        lDz = Val(Range(rngItem.Address).Offset(, 2))
        lCs = Val(Range(rngItem.Address).Offset(, 3))
        sUOM = Range(rngItem.Address).Offset(, 4)
    End If
    If lDz = 0 Or lCs = 0 Or sUOM = "" Then
        Call ErrorTrap
    End If
    
End Sub
Code:
Option Explicit
Public sUOM As String


Private Sub cmdbtnAddItem_Click()
    
    Dim LastRow As Long
    Dim cnt As Integer
    Dim ctl As Control
    
    Worksheets(Chattemfrm.cmbSDPFLine.Value).Select
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    
    Set rngItem = Range(rngItem.Address)
    
    If rngItem.Offset(, -1).Value = "" Then
        Cells(LastRow, 1).Value = Len(Me.txtbxDescription.Value)
    Else
        rngItem.Offset(, -1).Value = Len(Me.txtbxDescription.Value)
    End If
    
    If rngItem.Offset(, 0).Value = "" Then
        Cells(LastRow, 2).Value = Me.txtbxPrdctCde.Value
    Else
        rngItem.Offset(, 0).Value = Me.txtbxPrdctCde.Value
    End If
        
    If rngItem.Offset(, 1).Value = "" Then
        Cells(LastRow, 3).Value = Application.Proper(Me.txtbxDescription.Value)
    Else
        rngItem.Offset(, 1).Value = Application.Proper(Me.txtbxDescription.Value)
    End If
            
    If rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value Then
        Cells(LastRow, 4).Value = Me.txtbxDzPrCs.Value
    Else
        rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value
    End If
    
    If rngItem.Offset(, 3).Value = Me.txtbxCsPerPal.Value Then
        Cells(LastRow, 5).Value = Me.txtbxCsPerPal.Value
    Else
        Cells(LastRow, 3).Value = Me.txtbxCsPerPal.Value
    End If
    
    If frmAddProduct.txtbxDzPrCs.Value = "" Then
        MsgBox "Please enter dozens per case.", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Dozens per case"
        Me.txtbxDzPrCs.SetFocus
        Exit Sub
    Else
        rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value
    End If
    
    If frmAddProduct.txtbxCsPerPal.Value = "" Then
        MsgBox "Please enter cases per pallet.", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Cases per pallet"
        Me.txtbxCsPerPal.SetFocus
        Exit Sub
    Else
        rngItem.Offset(, 3).Value = Me.txtbxCsPerPal.Value
    End If
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "OptionButton" Then
            If ctl.Value = True Then
                cnt = cnt + 1
                Debug.Print "You have selected " & ctl.Caption
                rngItem.Offset(, 4).Value = ctl.Caption
                sUOM = ctl.Caption
            End If
        End If
    Next ctl
    If cnt = 0 Then
        MsgBox "Please select a unit of measure (UOM).", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Unit of Measure"
        frmAddProduct.optEa.SetFocus
        Exit Sub
    End If
    
    Unload Me
    frmSDPFLineSelect.Show
End Sub
Code:
Function findItem(Rng As Range, sPrdCde As String) As Range
    
    Set findItem = Rng.Find(sPrdCde, lookat:=xlPart)


End Function
Thank You
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
It looks to me as though you need something like this:

Code:
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
    
    If rngItem Is Nothing then Set rngItem = Cells(Lastrow, "B")

instead of this:

Code:
    Set rngItem = Range(rngItem.Address)
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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