Run Time Error 1004: Method 'Range' of object'_Worksheet' failed ... Help Please !!

davidsands007

New Member
Joined
Feb 5, 2016
Messages
1
Hiya, I've got a spreadsheet showing an interactive matrix whereby when the end user clicks on a cell, say with the number 4 in it, it will return the value of 4 peoples names in a msgbox. In order to achieve this I have the following code as a module (this part seems to be working correctly):

Function MYVLOOKUP(lookupval, lookuprange As Range, indexcol As Long)
Dim r As Range
Dim result As String
result = ""
For Each r In lookuprange
If r = lookupval Then
result = result & " " & Chr(10) & r.Offset(0, indexcol - 1)
End If
Next r
MYVLOOKUP = result
End Function

... and then the following code sits as part of sheet1:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim Rng1 As Range
Set Rng = Range("C9:N28")
If Intersect(Target, Rng) Is Nothing Then Exit Sub
MsgBox MYVLOOKUP("Y" & Range("L4") & Range("C4") & Range("B" & Target.Row).Value & Range(Target.Column & 8).Value, Sheets("Data").Range("A4:D2050"), 4), vbInformation
End Sub



It is the MsgBox line that I get the 'Run Time Error 1004: Method 'Range' of object'_Worksheet' failed' error for and I have no idea why. It is worth me saying I'm a total novice with coding and I have copied all of this from another excel document (which does work correctly).

Any help would be much appreciated.

Dave
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Dave. The Function needs some adjustment...
Code:
Function MYVLOOKUP(lookupval as long, lookuprange As Range, indexcol As Long) as String
This isn't right according to the function requirements..
Code:
MsgBox MYVLOOKUP("Y" & Range("L4") & Range("C4") & Range("B" & Target.Row).Value & Range(Target.Column & 8).Value, Sheets("Data").Range("A4:D2050"), 4), vbInformation
The function requires...
Code:
MsgBox MYVLOOKUP(some value, Sheets("Data").Range("A4:D2050"),4),vbInformation
So maybe "some value" should be...
Code:
"Y" + Range("L4") + Range("C4") + Range("B" & Target.Row).Value + Range(Target.Column & 8).Value
Put "Option Explicit" at the top of your code. U will find that "Rng" & "Rng1" is not properly declared. I have no idea what "Y" equals? Also U should properly identify all ranges...
Code:
Sheets("Sheet1").Range("C" & 4).Value
HTH. Dave
 
Last edited:
Upvote 0
You can't use this:
Code:
Range(Target.Column & 8).Value
because Target.Column returns a number not a letter. Use:
Code:
Cells(8, Target.Column).Value
instead.
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,654
Members
449,462
Latest member
Chislobog

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