PDA

View Full Version : Clear cell edit



mdmackillop
03-27-2007, 02:14 PM
The following code will write/clear "a" in a cell, but it leaves the insertion point in the cell. I've tried adding SendKeys "{ESC}" but to no effect. Any other suggestions?
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Count > 1 Then Exit Sub
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Exit Sub
End If
End Sub

lucas
03-27-2007, 03:56 PM
I use this Malcolm:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Count > 1 Then Exit Sub
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Target.Offset(0, 1).Select
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Target.Offset(0, 1).Select
Exit Sub
End If
End Sub

Tommy
03-27-2007, 04:39 PM
I tried not to post this I really really tried. I couldn't resist::devil2:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Count = 1 Then
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Target.Offset(0, 1).Select
ElseIf Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Target.Offset(0, 1).Select
End If
End If
End Sub

johnske
03-27-2007, 04:42 PM
another Alternative - use Target.Next.Select in place of Target.Offset(0, 1).Select as per Steve's code

Norie
03-27-2007, 04:50 PM
Why not try Cancel=True?

johnske
03-27-2007, 04:58 PM
Why not try Cancel=True? :thumb

lenze
03-27-2007, 07:17 PM
Why not try Cancel=True?
Right on Norie!!

lenze

mdmackillop
03-28-2007, 12:02 AM
That's what I was looking for Norie!
Thanks to all.

mdmackillop
03-29-2007, 11:59 AM
Also, turn off "Edit directly in cell" in Tools/Options/Edit as I would have discovered by reading the whole of the KB entry!

mvidas
03-29-2007, 01:06 PM
Is it possible to have target.count be more than 1 in a double click event?

mdmackillop
03-29-2007, 01:17 PM
It seems not. The code was copied from a KB Item.
BTW, in testing the poosibility, I discovered double clicking a top or left border focus goes to the top/left. Never new that shortcut.

Bob Phillips
03-29-2007, 01:17 PM
Don't think so.

geekgirlau
03-29-2007, 10:34 PM
Just tested in v2003 - you're right! I never knew this shortcut existed!