Run time error '91':Object variable or block variable not set

Predcoder

New Member
Joined
Jan 28, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Sub trig()

Dim rng As Range, cell As Range, h As Range, l As Range, nh As Range, c1 As Range, c2 As Range, bs As Integer, I As Integer, low As Currency, high As Currency, newlow As Currency, newhigh As Currency, flag As Integer, loop1 As Integer


Set rng = Range("L102:L662")
loop1 = 0
Set h = Range("A1")
For Each cell In rng
flag = 0
bs = cell.Value

If bs = 1 Then
loop1 = 0
high = cell.Offset(0, -7).Value
nh = cell.Offset(1, -7)
Set h = Range("nh:E662")
While loop1 = 0
For Each c1 In h

If c1.Offset(0, 7).Value = 0 Then
newhigh = c1.Value
If newhigh > high Then
cell.Value = 1
loop1 = 1
Exit For
End If
Else
cell.Value = 0
loop1 = 1
Exit For
End If

Next c1
Wend


Else
loop1 = 0

If bs = -1 Then
low = cell.Offset(0, -6).Value
nh = cell.Offset(1, -6)
Set l = Range("nh:F662")
While loop1 = 0
For Each c2 In l

If c1.Offset(0, 6).Value = 0 Then
newlow = c1.Value
If newlow < low Then
cell.Value = -1
loop1 = 1
Exit For
End If
Else
cell.Value = 0
loop1 = 1
Exit For
End If

Next c2
Wend
End If

End If

Next cell


End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You've declared nh as a Range.
 
Upvote 0
It is customary to actually ask a question (also stating where the error is) and not just paste your code in a thread, anyway I have amended the 2 nh that I can see and the line after both instances but haven't looked at the rest of your code or tested it.

Rich (BB code):
Sub trig()

    Dim rng As Range, cell As Range, h As Range, l As Range, nh As Range, c1 As Range, c2 As Range, bs As Integer, I As Integer, low As Currency, high As Currency, newlow As Currency, newhigh As Currency, flag As Integer, loop1 As Integer


    Set rng = Range("L102:L662")
    loop1 = 0
    Set h = Range("A1")
    For Each cell In rng
        flag = 0
        bs = cell.Value

        If bs = 1 Then
            loop1 = 0
            high = cell.Offset(0, -7).Value
            Set nh = cell.Offset(1, -7)
            Set h = Range(nh, Range("E662"))
            While loop1 = 0
                For Each c1 In h

                    If c1.Offset(0, 7).Value = 0 Then
                        newhigh = c1.Value
                        If newhigh > high Then
                            cell.Value = 1
                            loop1 = 1
                            Exit For
                        End If
                    Else
                        cell.Value = 0
                        loop1 = 1
                        Exit For
                    End If

                Next c1
            Wend


        Else
            loop1 = 0

            If bs = -1 Then
                low = cell.Offset(0, -6).Value
                Set nh = cell.Offset(1, -6)
                Set l = Range(nh, Range("F662"))
                While loop1 = 0
                    For Each c2 In l

                        If c1.Offset(0, 6).Value = 0 Then
                            newlow = c1.Value
                            If newlow < low Then
                                cell.Value = -1
                                loop1 = 1
                                Exit For
                            End If
                        Else
                            cell.Value = 0
                            loop1 = 1
                            Exit For
                        End If

                    Next c2
                Wend
            End If

        End If

    Next cell


End Sub
 
Upvote 0
made a few changes:
now, nh2 is string
nh1 = Range(cell).Offset(1, -7).Address
is showing error
 
Upvote 0
ERROR 1004: Method Range of objetc Global failed
code:


Sub trig()

Dim rng As Range, cell As Range, h As Range, l As Range, nh1 As String, nh2 As Range, c1 As Range, c2 As Range, bs As Integer, I As Integer, low As Currency, high As Currency, newlow As Currency, newhigh As Currency, flag As Integer, loop1 As Integer


Set rng = Range("L102:L662")
loop1 = 0

For Each cell In rng
flag = 0
bs = cell.Value

If bs = 1 Then
loop1 = 0
high = cell.Offset(0, -7).Value
nh1 = Range(cell).Offset(1, -7).Address <<ERROR

h = Range("nh1:E662")
While loop1 = 0
For Each c1 In h

If c1.Offset(0, 7).Value = 0 Then
newhigh = c1.Value
If newhigh > high Then
cell.Value = 1
loop1 = 1
Exit For
End If
Else
cell.Value = 0
loop1 = 1
Exit For
End If

Next c1
Wend


Else
loop1 = 0

If bs = -1 Then
low = cell.Offset(0, -6).Value
nh2 = Range(cell).Offset(1, -6).Address
Set l = Range("nh2:F662")
While loop1 = 0
For Each c2 In l

If c1.Offset(0, 6).Value = 0 Then
newlow = c1.Value
If newlow < low Then
cell.Value = -1
loop1 = 1
Exit For
End If
Else
cell.Value = 0
loop1 = 1
Exit For
End If

Next c2
Wend
End If

End If

Next cell


End Sub
 
Upvote 0
VBA Code:
Sub trig()

Dim rng As Range, cell As Range, h As Range, l As Range, nh1 As String, nh2 As Range, c1 As Range, c2 As Range, bs As Integer, I As Integer, low As Currency, high As Currency, newlow As Currency, newhigh As Currency, flag As Integer, loop1 As Integer


Set rng = Range("L102:L662")
loop1 = 0

For Each cell In rng
 flag = 0
 bs = cell.Value

 If bs = 1 Then
   loop1 = 0
   high = cell.Offset(0, -7).Value
   nh1 = Range(cell).Offset(1, -7).Address  <<error
   
   h = Range("nh1:E662")
      While loop1 = 0
        For Each c1 In h
         
         If c1.Offset(0, 7).Value = 0 Then
          newhigh = c1.Value
            If newhigh > high Then
            cell.Value = 1
            loop1 = 1
            Exit For
            End If
         Else
         cell.Value = 0
         loop1 = 1
         Exit For
         End If
         
        Next c1
      Wend


 Else
   loop1 = 0
   
   If bs = -1 Then
    low = cell.Offset(0, -6).Value
    nh2 = Range(cell).Offset(1, -6).Address
    Set l = Range("nh2:F662")
    While loop1 = 0
        For Each c2 In l
         
         If c1.Offset(0, 6).Value = 0 Then
          newlow = c1.Value
            If newlow < low Then
            cell.Value = -1
            loop1 = 1
            Exit For
            End If
         Else
         cell.Value = 0
         loop1 = 1
         Exit For
         End If
      
        Next c2
      Wend
    End If

 End If

Next cell


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,093
Members
448,944
Latest member
SarahSomethingExcel100

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