Worksheet Selection Change Event

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a worksheet with cell A5 displaying "CLICK ME TO SEE FILE PATH"

Then with the following code in the worksheet:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    If Target.Address = "$A$5" And Target.Value = "CLICK ME TO SEE FILE PATH" Then
        Target.Value = Application.ActiveWorkbook.FullName
    Else
        Cells(5, 1).Value = "CLICK ME TO SEE FILE PATH"
    End If
End Sub

The reason I added "On Error Resume Next" was because I was getting an error whenever I selected a cell with an error in it in the worksheet.

However with this added line of code it transforms my errors in to the file path.

How can I make it ignore the cells with errors in the worksheet?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]CLICK ME TO SEE FILE PATH[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]#DIV/0![/TD]
[/TR]
</tbody>[/TABLE]

If I click cell A5 I will see the file path, if I click A6 I will also see the file path, which I don't want to happen

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Y:\Client0152\30625\Excel\0152MappingDoc.xlsm[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Y:\Client0152\30625\Excel\0152MappingDoc.xlsm[/TD]
[/TR]
</tbody>[/TABLE]


I want A6 to still show #DIV/0! after clicking on it as the cell A6 will now just display the file path at all times - A5 resets itself to "CLICK ME TO SEE FILE PATH" when not clicked so that's fine.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi there. A quick solution would be to change the code to:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo exiter
    If Target.Address = "$A$5" And Target.Value = "CLICK ME TO SEE FILE PATH" Then
        Target.Value = Application.ActiveWorkbook.FullName
    Else
        Cells(5, 1).Value = "CLICK ME TO SEE FILE PATH"
    End If
exiter:
End Sub
 
Upvote 0
Use separate If statements:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$5" Then
        If Target.Value = "CLICK ME TO SEE FILE PATH" Then
            Target.Value = Application.ActiveWorkbook.FullName
        End If
    Else
        Cells(5, 1).Value = "CLICK ME TO SEE FILE PATH"
    End If
End Sub
 
Upvote 0
Thank you both for the solutions, both worked a charm.
 
Upvote 0
Youre welcome. BTW, I prefer John_w answer to mine.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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