VBA run when cell selected in range containing specfic text not working

p.huddy

Board Regular
Joined
May 5, 2010
Messages
59
Hi,

I am trying to write a code then when someone clicks on a cell in Range G8:G1007 if the selected cell value is 'Copy' it copys the cell 2 columns along on the row that was selected but I can't work it out! Any pointers?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("G8:G1007")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
            If ActiveCell.Value = "Copy" Then
            ActiveCell.Offset(, 2).Copy
            End If
        End If
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("G8:G1007")
    
    If Not Intersect(Target, KeyCells) Is Nothing Then
            If UCase(Target.Value) = "COPY" Then
            Target.Offset(, 2).Copy
            End If
        End If
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Not Intersect(Target, Range("G8:G1007")) Is Nothing Then
   If UCase(Target.Value) = "COPY" Then Target.Offset(0, 2).Copy
End If
End Sub
 
Upvote 0
Not sure why you would want to do what your asking for.
Why not just double click on any cell in the range you want to copy?
This script will copy the cell in the range("I8:I1007")
Just by double clicking on the cell.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
If Not Intersect(Target, Range("I8:I1007")) Is Nothing Then
Target.Copy
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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