error 91, Object Variable or With Block not set when calling second range /

peligroso

New Member
Joined
Mar 13, 2008
Messages
5
Hi All,

I have an issue when calling a second procedure from within a range find execution. I am getting a run-time error 91, Object Variable or With Block not set.

Basically I am
  • looping an array
  • using .find to search for the value in a column
  • Insert a number of rows after the value if found
  • Call procedure, execute another find to using range / with
    • Find another value / range and select
    • Copy from another worksheet a range into the blank rows....
  • continue searching
The problem is line 54 which calls the procedure to insert the range after the rows have been inserted. If this is commented out, the search works fine. If the call is made to insertBundleItems procedure, the range is copied and pasted however when execution returns I get the error when execution hits line 56 (first instance). The rng object is nothing at this point. I assume this is loss of the reference to the range however I am at a loss to understand why.

Help really appreciated.


1 sub parseBoxes()
2
3 ' Create search criteria and assign to range.
4 ' Search for boxes and insert rows to allow for box produce.
5
6 Dim vLookupSheet As Worksheet
7 Dim vDataSheet As Worksheet
8 Dim vStartRow As Integer
9 Dim vEndRow As Integer
10 Dim vGroupedItem As Variant
11 Dim vBundleItems As Range
12 Dim vLastRowBundles As Integer
13 Dim vLastRowOrders As Integer
14
15
16 Set vLookupSheet = Worksheets("Lookup")
17 Set vDataSheet = Worksheets("Raw Data")
18
19 With vLookupSheet.Range("box_list")
20 vLastRowBundles = .End(xlDown).Row
21 End With
22
23 With vDataSheet.Range("A:A")
24 vLastRowOrders = .End(xlDown).Row
25 End With
26
27 vBoxArray = vLookupSheet.Range("B6:D" & vLastRowBundles).Value
28
29 For I = LBound(vBoxArray) To UBound(vBoxArray)
30
31 vFullName = vBoxArray(I, 1)
32 vReplaceName = vBoxArray(I, 2)
33 vRowsToInsert = vBoxArray(I, 3)
34 vTestRng = vDataSheet.Range("Q2:Q10000")
35 With vDataSheet.Range("Q:Q")
36 'If you want to find a part of the rng.value then use xlPart
37 'if you use LookIn:=xlValues it will also work with a
38 'formula cell that evaluates to MySearch(I)
39 Set rng = .Find(What:=vFullName, _
40 After:=.Cells(.Cells.Count), _
41 LookIn:=xlValues, _
42 LookAt:=xlWhole, _
43 SearchOrder:=xlByRows, _
44 SearchDirection:=xlNext, _
45 MatchCase:=False)
46
47 If Not rng Is Nothing Then 'Execute if valid range found
48 FirstAddress = rng.Address
49 Do
50 vPasteCol = rng.Column
51 vStartRow = rng.Row + 1
52 vEndRow = vStartRow + vRowsToInsert - 1
53 vDataSheet.Rows(vStartRow & ":" & vEndRow).Insert Shift:=xlDown
54 Call insertBundleItems(vReplaceName, vRowsToInsert, vStartRow)
55 Set rng = .FindNext(rng)
56 Loop While Not rng Is Nothing And rng.Address <> FirstAddress
57 End If
58 End With
59
60 Next I
61
62 End Sub
63
64 Sub insertBundleItems(pBundleName As Variant, pArrayLength As Variant, pPasteRow As Variant)
65
66 'Get Bundle items for bundle name
67
68 Dim vBundleStart As Variant
69 Dim vBundleItems As Variant
70 Dim vBundleEndRange As Variant
71 Dim vPasteRange As String
72
73 With Sheets("Lookup").Range("box_items")
74 Set vBundle = _
75 .Find(What:=pBundleName, _
76 LookIn:=xlValues, _
77 LookAt:=xlWhole, _
78 SearchOrder:=xlByRows, _
79 SearchDirection:=xlNext, _
80 MatchCase:=False)
81 If Not vBundle Is Nothing Then
82 vEndRow = vBundle.Row + (pArrayLength - 1)
83 vBundleColumn = Split(Cells(1, vBundle.Column + 2).Address, "$")(1) 'Convert number to letter
84 vBundleRow = vBundle.Row
85 'vBundleRange = (Left(vBundle.Address, Len(vBundle.Address))) & ":" & "$K$" & vEndRow
86 vBundleRange = (vBundleColumn & vBundleRow & ":" & "K" & vEndRow)
87 Sheets("Lookup").Range(vBundleRange).Copy Sheets("Raw Data").Range("Q" & pPasteRow)
88 End If
89 End With
90
91 End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Reformatted sop its more readable....


VBA Code:
01 sub parseBoxes()
02
03   ' Create search criteria and assign to range.
04   ' Search for boxes and insert rows to allow for box produce.
05
06    Dim vLookupSheet As Worksheet
07    Dim vDataSheet As Worksheet
08    Dim vStartRow As Integer
09    Dim vEndRow As Integer
10    Dim vGroupedItem As Variant
11    Dim vBundleItems As Range
12    Dim vLastRowBundles As Integer
13    Dim vLastRowOrders As Integer
14
15
16    Set vLookupSheet = Worksheets("Lookup")
17    Set vDataSheet = Worksheets("Raw Data")
18
19    With vLookupSheet.Range("box_list")
20       vLastRowBundles = .End(xlDown).Row
21    End With
22
23    With vDataSheet.Range("A:A")
24       vLastRowOrders = .End(xlDown).Row
25    End With
26
27    vBoxArray = vLookupSheet.Range("B6:D" & vLastRowBundles).Value
28
29    For I = LBound(vBoxArray) To UBound(vBoxArray)
30
31       vFullName = vBoxArray(I, 1)
32       vReplaceName = vBoxArray(I, 2)
33       vRowsToInsert = vBoxArray(I, 3)
34       vTestRng = vDataSheet.Range("Q2:Q10000")
35       With vDataSheet.Range("Q:Q")
36          'If you want to find a part of the rng.value then use xlPart
37          'if you use LookIn:=xlValues it will also work with a
38          'formula cell that evaluates to MySearch(I)
39          Set rng = .Find(What:=vFullName, _
40            After:=.Cells(.Cells.Count), _
41            LookIn:=xlValues, _
42            LookAt:=xlWhole, _
43            SearchOrder:=xlByRows, _
44            SearchDirection:=xlNext, _
45            MatchCase:=False)
46
47            If Not rng Is Nothing Then 'Execute if valid range found
48                FirstAddress = rng.Address
49                Do
50                         vPasteCol = rng.Column
51                         vStartRow = rng.Row + 1
52                         vEndRow = vStartRow + vRowsToInsert - 1
53                         vDataSheet.Rows(vStartRow & ":" & vEndRow).Insert Shift:=xlDown
54                         Call insertBundleItems(vReplaceName, vRowsToInsert, vStartRow)
55                         Set rng = .FindNext(rng)
56                Loop While Not rng Is Nothing And rng.Address <> FirstAddress
57             End If
58       End With
59
60    Next I
61
62 End Sub
63
64 Sub insertBundleItems(pBundleName As Variant, pArrayLength As Variant, pPasteRow As Variant)
65
66    'Get Bundle items for bundle name
67
68    Dim vBundleStart As Variant
69    Dim vBundleItems As Variant
70    Dim vBundleEndRange As Variant
71    Dim vPasteRange As String
72
73    With Sheets("Lookup").Range("box_items")
74       Set vBundle = _
75       .Find(What:=pBundleName, _
76          LookIn:=xlValues, _
77          LookAt:=xlWhole, _
78          SearchOrder:=xlByRows, _
79          SearchDirection:=xlNext, _
80          MatchCase:=False)
81          If Not vBundle Is Nothing Then
82             vEndRow = vBundle.Row + (pArrayLength - 1)
83             vBundleColumn = Split(Cells(1, vBundle.Column + 2).Address, "$")(1) 'Convert number to letter
84             vBundleRow = vBundle.Row
85             'vBundleRange = (Left(vBundle.Address, Len(vBundle.Address))) & ":" & "$K$" & vEndRow
86             vBundleRange = (vBundleColumn & vBundleRow & ":" & "K" & vEndRow)
87             Sheets("Lookup").Range(vBundleRange).Copy Sheets("Raw Data").Range("Q" & pPasteRow)
88          End If
89      End With
90
91 End Sub
 
Upvote 0
I've tried replicating the section that is causing the error and not had any issues with rng reverting to nothing.
Noting that you appear to have declared all of your variables with the exception of rng and thinking beyond what we can see, is rng declared elsewhere as a public variable and perhaps being cleared by another procedure that is being triggered? You haven't disabled events in your code so a worksheet change event or similar could be firing and setting rng to nothing.
 
Upvote 0
I get the error when execution hits line 56 (first instance). The rng object is nothing at this point. I assume this is loss of the reference to the range however I am at a loss to understand why.

Hi,
try changing these lines in your code

VBA Code:
55 Set Rng = .FindNext(Rng)
56  Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress


to this

VBA Code:
55 Set Rng = .FindNext(Rng)
56 If Rng Is Nothing Then Exit Do
57  Loop While Rng.Address <> FirstAddress

and see if this helps you

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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