Insert & remove data into a cell by selecting it

The Dutchman

Board Regular
Joined
Apr 10, 2008
Messages
72
I have a named range called "fill_range" that, when you click on any cell in this range it will either place an "X" in the cell or if the cell has contents, clear the contents.

The code I have which fives me an End If blocking error message is...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
If Not Intersect(Target, Range("fill_area")) Is Nothing Then
        If Not (IsEmpty(ActiveCell)) Then ActiveCell.ClearContents
            Else
                ActiveCell.Value = "X"
        End If
End If
 
End

Thx
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
If Not Intersect(Target, Range("fill_area")) Is Nothing Then
        If Not (IsEmpty(ActiveCell)) [COLOR=red]Then ActiveCell.ClearContents[/COLOR]
            Else
                ActiveCell.Value = "X"
        End If
End If
 
End

The red portion is causing the End If error. It should look like this

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
If Not Intersect(Target, Range("fill_area")) Is Nothing Then
        If Not [COLOR=black]IsEmpty(Target) Then [/COLOR]
[COLOR=black]               Target.ClearContents[/COLOR]
            Else
                [COLOR=black]Target[/COLOR].Value = "X"
        End If
End If
 
End Sub

Also, in your OP you said your range was called "fill_range" but you put "fill_area". Not sure if this is just a typing error.

I edited some of your stuff too.
 
Upvote 0
For instance:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim rng As Range
    Dim rngIntersection As Range

    Set rngIntersection = Intersect(Target, Range("fill_area"))

    If Not rngIntersection Is Nothing Then
        For Each rng In rngIntersection
            If Len(rng.Value) Then
                rng.ClearContents
            Else
                rng.Value = "X"
            End If
        Next
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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