VBA Copy and Paste

Yoepy

New Member
Joined
Feb 22, 2015
Messages
44
The following code does one of three things
1. Copies
3. Pastes
2. If copy and paste have already been done, the Double click transfers you to the event sheet.

I use a counter in "A1" to establish what we are doing (1 or zero)

My problem is, that if someone starts the process, with a zero and therefore is copying, if they hit the Esc key before the second double click (the Paste) the counter doesn't get reset.

How do I include
Range ("A1") = 0
if the ESC key is hit.

Thank you.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const WS_RANGE As String = "B:B"
Const WS_DESTN As String = "G6:K68"
Dim WsName As String

On Error GoTo ws_exit
Cancel = True 'rid protection error

'Copy event
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
            If Range("A1") = 0 Then
                With Target
                    ActiveCell.Copy
                End With
            Range("A1") = 1
            End If
GoTo ws_exit

Else
    If Not Intersect(Target, Me.Range(WS_DESTN)) Is Nothing Then
    
'Go to the event
        If Range("A1") = 0 Then
            WsName = Target.Value
                With Target
                    Sheets(WsName).Visible = True
                    Sheets(WsName).Select
                End With
        Else
    
'Paste event
    If Range("A1") = 1 Then
                With Target
                    ActiveCell.PasteSpecial xlPasteValues
                End With
            Application.CutCopyMode = False
    End If
    End If
    End If
            Range("A1") = 0 'Reset counter
    End If

ws_exit:

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
To disable the ESC key : Application.OnKey "{ESC}", ""
To enable the ESC key : Application.OnKey "{ESC}"
 
Upvote 0
Thanks footoo, but I don't want to disable. I just want to reset my counter if the ESC key is used.
 
Upvote 0
Try this :
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const WS_RANGE As String = "B:B"
Const WS_DESTN As String = "G6:K68"
Dim WsName As String


[COLOR=#ff0000]'On Error GoTo ws_exit[/COLOR]
Cancel = True    'rid protection error




'Copy event
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    If Range("A1") = 0 Then
        With Target
            ActiveCell.Copy
        End With
        Range("A1") = 1
    End If
    GoTo ws_exit


Else
    If Not Intersect(Target, Me.Range(WS_DESTN)) Is Nothing Then


        'Go to the event
        If Range("A1") = 0 Then
            WsName = Target.Value
            With Target
                Sheets(WsName).Visible = True
                Sheets(WsName).Select
            End With
        Else


            'Paste event
[COLOR=#ff0000]            On Error Resume Next[/COLOR]
            If Range("A1") = 1 Then
                With Target
                    ActiveCell.PasteSpecial xlPasteValues
                End With
                Application.CutCopyMode = False
            End If
[COLOR=#ff0000]            If Err > 0 Then Range("A1") = 0[/COLOR]
        End If
        Range("A1") = 0     'Reset counter
    End If
End If
ws_exit:
End Sub
 
Upvote 0
Thanks again footoo, it works, but it still means the user needs to double click in the WS_DESTN range for the reset to happen.
It's more likely to be needed when they realise they have clicked the wrong one in the WS_RANGE and then ESC.

But I'll play with your idea.
 
Upvote 0
A bit of playing with the order and I have it working.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const WS_RANGE As String = "B6:B104"
Const WS_DESTN As String = "G6:K68"
Dim WsName As String

On Error GoTo ws_exit
Cancel = True 'rid protection error

'Copy event
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

            If Range("A1") = 1 Then 'Error handler
                Range("A1") = 0 'Reset counter
            End If

            If Range("A1") = 0 Then
                With Target
                    ActiveCell.Copy
                    Range("A1") = 1
                End With
            End If
GoTo ws_exit

Else

If Not Intersect(Target, Me.Range(WS_DESTN)) Is Nothing Then
'Go to the event
        If Range("A1") = 0 Then
            WsName = Target.Value
                With Target
                    Sheets(WsName).Visible = True
                    Sheets(WsName).Select
                End With
        Else
    
'Paste event
            If Range("A1") = 1 Then
                Range("A1") = 0 'Reset counter
                    With Target
                        ActiveCell.PasteSpecial xlPasteValues
                    End With
                Application.CutCopyMode = False
            End If
        End If
    End If
End If

ws_exit:

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,483
Messages
6,125,065
Members
449,206
Latest member
Healthydogs

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