PDA

View Full Version : Solved: BeforeDoubleClick Issue?



Ctrl
10-02-2009, 05:40 AM
Hello,

I wrote this code to make it easier to organize rows in the active sheet. So with one double click the current row is cut-selected and with another double click the row will be inserted. I used Range("B1") value to differentiate between the first double click and the second double click. But the problem is when you hit Esc to cancel the selection (i.e. the first double click) the second double click is always carried out because the value of B1 is the same.
How can I differentiate between the 1st and 2nd double clicks without having to set B1 value?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Dim RwNum As Long
Dim DbCk As Integer

DbCk = Range("B1").Value

If ActiveCell.Value = "Not This Cell" Or ActiveCell.Row = 1 Then
Exit Sub
End If

Select Case DbCk

Case Is = 0
RwNum = ActiveCell.Row
Rows(RwNum).Select
Selection.Cut
Range("B1").Value = 1

Case Is = 1
Selection.Insert Shift:=xlDown
Range("B1").Value = 0

End Select

End Sub

Bob Phillips
10-02-2009, 06:44 AM
Probably off-beam as i don't uderstand what you are trying to do



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static DbCk As Boolean

If Target.Value = "Not This Cell" Or Target.Row = 1 Then
Exit Sub
End If

If Not DbCk Then

Target.EntireRow.Cut
Else
Target.EntireRow.Insert Shift:=xlDown
End Select
DbCk = not DbCk

Cancel = True

End Sub

Ctrl
10-02-2009, 08:10 AM
Simply, what I want to do is cutting and inserting individual rows just by double clicking instead of having to (select the entire row then right clicking then choosing 'cut' command. Then selecting another row then right clicking then choosing 'insert cut cells' command).

1st double click will cut the entire row, the 2nd double click will insert it.
with the possibility to cancel the cut process after the 1st double click anytime so that the 2nd double click (inserting the row) is ignored.

I hope I made myself clear, thank you xld.

Ctrl
10-02-2009, 09:54 AM
Thank you xld.
Your code actually works exacatly the way I want it. :friends:
I didn't notice that first because it gave an error.

End Select must be End If
It's my bad anyway.

Your help is appreciated.

Bob Phillips
10-02-2009, 10:11 AM
Sorry about that, I just cut it from the original, and didn't test it.

Ctrl
10-03-2009, 02:03 AM
Now, it's more perfect. I figured out how to cancel the 2nd double click effect in case the Esc button was pressed after the 1st double click.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static DbCk As Boolean

If Target.Value = "Not This Cell" Or Target.Row = 1 Then Exit Sub

If Not DbCk Then
Target.EntireRow.Cut
Else
If Not Application.CutCopyMode = xlCut Then
DbCk = Not DbCk
Exit Sub
Else
Target.EntireRow.Insert Shift:=xlDown
End If
End If

DbCk = Not DbCk
Cancel = True

End Sub