Reputation: 17
I am working on a code project in which I need to match a value from one cell in column G in one sheet (in this case 'test sheet') to a value from a cell in column B in another sheet (in this case 'Information'). Once I have found this matching row in the 'Information' sheet, I need to retrieve the integer value from column C in that matching row.
I am trying to implement this code in a function that I could call from a different part of my program. I have tried dimensioning my variables in many different ways, but I am stuck with a type mismatch that occurs when I call my function from my main sub. I am very lost and frustrated. Can any of you see what I am doing incorrectly?
The main sub call:
Sub AutoSend()
Dim Temp As Integer
Dim rng As Range
Dim r As Range
Set rng = Range("A2:I6")
For Each r in rng.Rows 'EDIT to more accurately reflect my code
Temp = FindHigh(Cells(r,7).Value) 'THROWS ERROR HERE
Next r
End Sub
The function FindHigh that is called:
Function FindHigh(Key As String) As Integer
Dim Target
Dim Success As Integer
Success = 0
Sheets("Information").Select
Set Target = Columns(2).Find(Key, LookIn:=xlValues)
If Not Target Is Nothing Then
Sheets("Information").Cells(Target.row, 3).Select
Success = Rows(Target.RowIndex).Cells(Target.RowIndex, 3).Value
End If
Sheets("Test Sheet").Select
FindHigh = Success
End Function
Upvotes: 0
Views: 1293
Reputation: 8003
You could give this a try.
Sub AutoSend()
Dim Temp As Integer
Dim r As Integer
r = 1 'Row Number of the Row in column G that has the value you want to look up.
With WorksheetFunction
Temp = .IfError(.VLookup(Sheets("Test Sheet").Cells(r, 7).Value, _
Sheets("Information").Range("B:C"), 2, False), 0)
End With
End Sub
If the cell that has the value you are looking for is NOT in column G, or comes from a loop telling us what cell holds the value you would like to look up, OR giving us the code to your loop would be very helpful in giving you a better answer.
Upvotes: 0
Reputation: 11905
In your Sub "AutoSend" you are using 'r' that hasn't been defined anywhere
Upvotes: 0
Reputation: 5719
Maybe it should be Temp = FindHigh(Range("R7").Value)
Or Temp = FindHigh(Cells(7,18))
Upvotes: 1