Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem w excel hyperlink and vba code

Status
Not open for further replies.

mmogul

IS-IT--Management
Dec 1, 2003
218
US
I have a workbook with 2 worksheets. If a user clicks within a particular column, the second worksheet is activated and the data is filtered based on the value of the cell in worksheet 1. I use the function InRange (see code) to see if the user has clicked in the designated column in order to activate the code.

I also have another column where the user can click a hyperlink. When this is done, a specific workbook is opened based on values on that row.

The problem I am having is that when the user clicks on the hyperlink the first time, the code fails with an error:

Run time error '1004'
Method 'Intersect' of object '_Application' failed.

The second time the user clicks on the same hyperlink, the code works.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim sStatementID As String


If InRange(ActiveCell, Range("LinkColumn")) Then
    sStatementID = ActiveCell.Value
    Call FilterTo1Criteria(sStatementID)
Else
End If

End Sub

Sub FilterTo1Criteria(sStatementID)

Application.Sheets("Summary").Activate

With Sheet2

            .AutoFilterMode = False

            .Range("A6:e6").AutoFilter

            .Range("A6:e6").AutoFilter Field:=4, Criteria1:=sStatementID
            
End With

    

End Sub

Function InRange(Range1 As Range, Range2 As Range) As Boolean
' returns True if Range1 is within Range2
Dim InterSectRange As Range
    Set InterSectRange = Application.Intersect(Range1, Range2)
    InRange = Not InterSectRange Is Nothing
    Set InterSectRange = Nothing
End Function

The hyperlink formula on the spreadsheet is:

=IF(J22="","",HYPERLINK(K22,"Workbook"))

Thanks.





 

hi,
Code:
If InRange([b]Target[/b], Range("LinkColumn")) Then
NOT activecell!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


BTW,

This could also be coded...
Code:
If [b]Not Intersect(Target, Range("LinkColumn")) Is Nothing[/b] Then


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top