The main purpose of this function is to check if the targetCell(the cell I doubleclicked[sic]) is found within the stored range.address
You are looking to see if the Intersect method produces something that is within the range described by the text in a cell (e.g. AQ43:BF44 ) and a cell you double-click (e.g. Target ).
Option Explicit '<~~ see footnote
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim pRange As String
pRange = Worksheets("Sheet1").Range("A1").Value2
'alternates
'pRange = Worksheets("Sheet1").Range("A1").value
'pRange = Worksheets("Sheet1").Range("A1").text
If Not Intersect(Target, Range(pRange)) Is Nothing Then
Cancel = False
MsgBox Target.Address(0, 0) & " is within " & pRange
End If
End Sub
You are seeking to retrieve text from a cell; possibly text that is returned from a formula involving something like the ADDRESS function. For this you can use the Range.Value property, Range.Text property or Range.Value2 property. Strictly speaking, the .Value2 property is the fastest but it isn't going to make a lot of difference here.
It's a little unclear on where the text (e.g. AQ43:BF44 ) resides. I've included parent worksheet as well as range definition that should be easy to edit.
A global solution could be achieved with the ThisWorkbook's Workbook_SheetBeforeDoubleClick event macro.
Setting Require Variable Declaration within the VBE's Tools ► Options ► Editor property page will put the Option Explicit statement at the top of each newly created code sheet. This will avoid silly coding mistakes like misspellings as well as influencing you to use the correct variable type in the variable declaration. Variables created on-the-fly without declaration are all of the variant/object type. Using Option Explicit is widely considered 'best practice'.
pRange = projectRange.Valueand notpRange = Project.Range