Run time error '91'

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi i have the code below but i keep getting a runtime error 91 object variable or with block variable not set, please can you hellp with this it highlights in yellow when i click on debug the .Formula = TheFormula code which is below in the whole code, any advise would be great and thanks

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range
    Static Cell As Range
    Static TheFormula As String
    Set Rng = Range("N2:V192")
    If Not Application.Intersect(Target, Rng) Is Nothing Then
        If Not Cell Is Nothing Then
            Cell.Formula = TheFormula
        End If
        Set Cell = ActiveCell
        With Cell
            TheFormula = .Formula
            .Value = .Value
        End With
    Else
        With Cell
            .Formula = TheFormula
        End With
    End If
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When you open the workbook and select, lets say, A1 it will error because "Cell" is not set to anything but you are trying to treat is an object that is set to something.

I think your errors would go if you changed Cell with Target and removed Set Cell = ActiveCell also. Target is the activecell you just selected so no need to create another object for the activecell.
 
Upvote 0
Hi thanks for the help i have just changed the code to the following...but now when i click on the cell the data gets deleted. can i have some more advise please ;)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range
    Static Cell As Range
    Static TheFormula As String
    Set Rng = Range("N2:V192")
    If Not Application.Intersect(Target, Rng) Is Nothing Then
        If Not Cell Is Nothing Then
            Target.Formula = TheFormula
        End If
        With Target
            TheFormula = .Formula
            .Value = .Value
        End With
    Else
        With Target
            .Formula = TheFormula
        End With
    End If
End Sub
 
Upvote 0
you still have Cell referenced.

this:
If Not Cell Is Nothing Then

should be this:
If Not Target Is Nothing Then

and the Static Cell as range can be removed as well.
 
Upvote 0
hi i have now changed the code to the following... but it is still deleting the data when a cell is clicked on, please help. I am still quite new to this coding and still learning. :)
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range
    Static TheFormula As String
    Set Rng = Range("N2:V192")
    If Not Application.Intersect(Target, Rng) Is Nothing Then
        If Not Target Is Nothing Then
            Target.Formula = TheFormula
        End If
        With Target
            TheFormula = .Formula
            .Value = .Value
        End With
    Else
        With Target
            .Formula = TheFormula
        End With
    End If
End Sub
 
Last edited:
Upvote 0
Just to clarify, what is it that you want the code to do?
 
Upvote 0
you want to essentially paste values over the formula so people cant see the formula in the N:V range?

does this do what you want?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Rng As Range
    Set Rng = Range("N2:V192")
    If Not Application.Intersect(Target, Rng) Is Nothing Then
        If Not Target Is Nothing Then
            Target.Value = Target.Value
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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