Function returns #VALUE but works in immediate window

Sanne97

New Member
Joined
Apr 21, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to make a function where, based on an article code, the funciton looks in a different sheet with matching codes and takes the right discount (5 columns to the right of the cell with the code).

Then I would like to return a specific value when the discount is set as "standaard".

Everything seems fine in the immediate window, but when I enter the formula in a cell, It returns #VALUE.

Here is the code:
VBA Code:
Function Discount()

Application.Volatile True

Dim c As Variant
Dim cl As Range
Dim r As Integer
Dim a As String
r = ActiveCell.Row
a = Sheets(1).Cells(r, 3).Value


Sheets(2).Activate
    With Sheets(2).Range("B3:B40000")
        Set cl = Sheets(2).Range("B2:B10000").Find(a, LookAt:=xlWhole)
        If Not cl Is Nothing Then
            cl.Select
        End If
    End With


If ActiveCell.Offset(0, 5).Value = "standaard" Then
    c = 1
        End If

  Discount = c

End Function
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Welcome to the forum. :)

You can't select a cell in a UDF. What should the result be if the value isn't found, or if the cell 5 to the right doesn't contain "standaard"?
 
Upvote 0
Thank you!

What i struggle with is that i need the value from "a", look for a matching value in sheet 2, then go five columns to the right and look at that value. If that value is "standaard", then it should refer to a different cell that contains the discount value.
 
Upvote 0
I solved the issue with the following code:



Public Function Bruto() As Variant

Application.Volatile True

r = Application.Caller.Row
b = Cells(r, 20).Value

If WorksheetFunction.Index(Sheets(2).Range("G:G"), _
WorksheetFunction.Match(Sheets(1).Cells(r, 3).Value, Sheets(2).Range("B:B"), 0)) = "standaard" Then
Bruto = "ST"

ElseIf WorksheetFunction.Index(Sheets(2).Range("G:G"), _
WorksheetFunction.Match(Sheets(1).Cells(r, 3).Value, Sheets(2).Range("B:B"), 0)) <> "standaard" Then
Bruto = b * (1 - WorksheetFunction.Index(Sheets(2).Range("G:G"), _
WorksheetFunction.Match(Sheets(1).Cells(r, 3).Value, Sheets(2).Range("B:B"), 0)))
End If

End Function
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,991
Members
452,541
Latest member
haasro02

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