DavidVernon
Board Regular
- Joined
- Jun 30, 2006
- Messages
- 71
Hi I get runtime error 1004, Activate method of Range Class failed when I run the macro below using a double click from a sheet called "Europe".
I have highlighted the code where the error occurs - any ideas anyone?
Private Sub Workbook_SheetBeforedoubleclick(ByVal Tgt As Range, Cancel As Boolean)
'define the sheet name as a string
Dim CountryName As String
'make sure the target cell has a country name associated with it
If Cells(Tgt.Row, 4) <> "" And Tgt.Column < 21 Then
CountryName = Cells(Tgt.Row, 3)
'tell the user if there is a problem
Else
MsgBox "No valid metric selected"
End If
'ok, so we are looking for the contents of the double-clicked cell...
SearchVal1 = Cells(Tgt.Row, Tgt.Column)
' target may have a lot of decimal places so the search will fail
' so we need to include a second searchval with one decimal place
Cells(4, 1) = SearchVal1
SearchVal2 = Round(SearchVal1, 1)
Cells(4, 2) = SearchVal2
' then activate the correct worksheet where the value is to be searched for
Worksheets(CountryName).Activate
' search for the value and activate the cell
Cancel = False
' THIS IS THE PROBLEM SECTION:
Cells.Find(SearchVal2, LookIn:=xlValues, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End Sub
I have highlighted the code where the error occurs - any ideas anyone?
Private Sub Workbook_SheetBeforedoubleclick(ByVal Tgt As Range, Cancel As Boolean)
'define the sheet name as a string
Dim CountryName As String
'make sure the target cell has a country name associated with it
If Cells(Tgt.Row, 4) <> "" And Tgt.Column < 21 Then
CountryName = Cells(Tgt.Row, 3)
'tell the user if there is a problem
Else
MsgBox "No valid metric selected"
End If
'ok, so we are looking for the contents of the double-clicked cell...
SearchVal1 = Cells(Tgt.Row, Tgt.Column)
' target may have a lot of decimal places so the search will fail
' so we need to include a second searchval with one decimal place
Cells(4, 1) = SearchVal1
SearchVal2 = Round(SearchVal1, 1)
Cells(4, 2) = SearchVal2
' then activate the correct worksheet where the value is to be searched for
Worksheets(CountryName).Activate
' search for the value and activate the cell
Cancel = False
' THIS IS THE PROBLEM SECTION:
Cells.Find(SearchVal2, LookIn:=xlValues, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End Sub