User Defined Function Call Error

dmelt253

New Member
Joined
Jan 12, 2016
Messages
8
So this is related to an earlier post I submitted. Someone was very kind enough to give me a function that did exactly what I wanted. However the only way that I can call this function is by creating it as an add-in. I would much rather use this in a macro enabled workbook because then other people can utilize this Macro without installing the add-in.

I guess I just don't understand where to put the function or exactly how to call it in my sub. Here is an excerpt of the code I have since the whole Macro is Very long.

Code:
Option Explicit

Public Function AreaOwner(BinName As String) As Variant
    Dim iBins As Long
     
    AreaOwner = CVErr(xlErrRef)
     
    With Worksheets("AreaOwnerKey")
        For iBins = 2 To .Rows.Count
            With .Rows(iBins)
                If UCase(.Cells(1).Value) <= UCase(BinName) And UCase(BinName) <= UCase(.Cells(2).Value) Then
                    AreaOwner = .Cells(3).Value
                    Exit Function
                ElseIf Len(.Cells(1).Value) = 0 Then
                    Exit Function
                End If
            End With
        Next iBins
    End With
End Function

Private Sub AutomatedPickLog()
Range("E1").Select
    ActiveCell.FormulaR1C1 = "=AreaOwner(RC[-1])"
    Selection.AutoFill Destination:=Range(Range("E1"), Range("F1").End(xlDown).Offset(0, -1)), Type:=xlFillDefault
End Sub

Basically I am trying to use the Function like a formula that evaluates D1 and puts the answer E1 and then repeats this step for all other cells in column D. However when I run this Macro the function results in a ?NAME error.

Is there a better way to do this?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You can put this code into any module of workbook - it's not tied to add-in.
 
Upvote 0
You can put this code into any module of workbook - it's not tied to add-in.

So why would this function work as an add-in but not as a function in a module? Is it because I'm trying to call it as a formula? I tried calling just the value
Code:
Cells(1, 5).Value = AreaOwner(Cells(1, 4))

And now I am getting a Type mismatch (error 13).
 
Upvote 0
you're passing the function a range when it's expecting a string.

try this instead

Code:
Cells(1, 5).Value = AreaOwner(Cells(1, 4).value)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,807
Members
449,468
Latest member
AGreen17

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