Make code apply to more than 1 column

C

Cheryl

I am using this code to insert X in a column with a mouse click. I need this
to apply to other column as well. How do I change this code?

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 5 Then
If Len(Trim(Target.Value)) = 0 Then
Target.Value = "X"
Else
Target.ClearContents
End If
End If
End Sub
 
D

Don Guillett

Are you SURE you want this with a SELECTION change? Maybe a double click
instead.

or target.column = 12
 
M

Mike H

Hi,

How about this which now does columns A,B & E. Change ot it match the
columns you want

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A:A,B:B,E:E")) Is Nothing Then
If Len(Trim(Target.Value)) = 0 Then
Target.Value = "X"
Else
Target.ClearContents
End If
End If
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
C

Cheryl

Thank You Mike it works!!

Mike H said:
Hi,

How about this which now does columns A,B & E. Change ot it match the
columns you want

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A:A,B:B,E:E")) Is Nothing Then
If Len(Trim(Target.Value)) = 0 Then
Target.Value = "X"
Else
Target.ClearContents
End If
End If
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
C

Cheryl

You are correct a double right mouse click would be better. How do I do that
change?
 
D

Don Guillett

right click sheet tab>view code>left window select worksheet>right window
select double_click>copy your code there..................
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top