G
Guest
i am trying to write a function to return the cost of samples
the arguments are the range of cells that either contain "Sold" or "Sample"
text (in my case K26:K39)
the function declaration so far is:
<BEGIN VB CODE>
Public Function CostOfSamples(s As Range)
' calculate and return the cost of samples
Dim pricing As Range
Set pricing = Worksheets("Master Price List").Range("B7:044")
CostOfSamples = 0
For Each cellVal In s.Cells
If cellVal.Value = "Sample" Then
CostOfSamples = CostOfSamples +
Application.WorksheetFunction.VLookup(cellVal.Offset(0, -8).Value, pricing,
6, False) * cellVal.Offset(0, -5).Value
End If
Next
End Function
<END VB CODE>
this constantly returns a #VALUE when used in excel
what am i missing?
i tested this using
CostOfSamples = cellVal.Offset(0,-5).Value
and it returned the correct value, but when i added the vlookup it stops.
also, (being new to vba) am i correct in thinking that in each iteration of
the loop, cellVal wil update to the next cell in the range 's'?
thanks!
the arguments are the range of cells that either contain "Sold" or "Sample"
text (in my case K26:K39)
the function declaration so far is:
<BEGIN VB CODE>
Public Function CostOfSamples(s As Range)
' calculate and return the cost of samples
Dim pricing As Range
Set pricing = Worksheets("Master Price List").Range("B7:044")
CostOfSamples = 0
For Each cellVal In s.Cells
If cellVal.Value = "Sample" Then
CostOfSamples = CostOfSamples +
Application.WorksheetFunction.VLookup(cellVal.Offset(0, -8).Value, pricing,
6, False) * cellVal.Offset(0, -5).Value
End If
Next
End Function
<END VB CODE>
this constantly returns a #VALUE when used in excel
what am i missing?
i tested this using
CostOfSamples = cellVal.Offset(0,-5).Value
and it returned the correct value, but when i added the vlookup it stops.
also, (being new to vba) am i correct in thinking that in each iteration of
the loop, cellVal wil update to the next cell in the range 's'?
thanks!