VBA to test if ActiveCell is in a specific column of a ListObject table before performing an action

team_qq

New Member
Joined
Jan 20, 2011
Messages
3
I'm trying to create a test before allowing a DoubleClick function to ensure that the DoubleClick only occurs in a particular column (the first column) of a table ("Table9"), before copying the contents of the ActiveCell that was clicked to a cell (C4) on a different worksheet.

The code below errors out with my test, that is attempting to establish a logic function to test if the Active Cell is in Column 1 of the ListObject Table 9.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(ActiveCell, ActiveSheet.ListObjects("Table9").ListColumns(1).DataBodyRange) Then
ActiveCell.Copy Worksheets("Doctor Detail").Range("C4")
Worksheets("Doctor Detail").Activate
End If

End Sub


I'd appreciate any suggestions how to make this function properly.

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
    On Error GoTo Escape
    Application.EnableEvents = False
    If Not Intersect(Me.ListObjects("Table9").ListColumns(1).DataBodyRange, Target) Is Nothing Then
        cancel = True
        Target.Copy Worksheets("Doctor Detail").Range("C4")
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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