Convert a SUB to a FUNCTION?

insaneoctane

Board Regular
Joined
Dec 2, 2005
Messages
72
This SUB works:
Code:
Public Sub getData()
    uid = "1012500"
    dept = Empty
    Dim wBook As Workbook
    Dim Dir_MRange As String
    Dim DirectoryRange As Variant
    Set wBook = Workbooks.Open("C:\Users\myuser\Documents\REFERENCE\SAP\SAS Directory 05-2012.xlsx") ' Path to external
    Dir_MRange = "B2:B14740"
    Dir_Range = "A2:K14740"
    uidrow = Application.WorksheetFunction.Match(uid, wBook.Worksheets("Directory").Range(Dir_MRange), 0)
    dept = wBook.Worksheets("Directory").Range("I" & uidrow + 1).Offset(0, 0).Value
    wBook.Close
    Set wBook = Nothing
End Sub

I'd like to make it a FUNCTION. I've tried but can't make it work. Any suggestions?
This is the function I tried to call the SUB that doesn't work...first I change the sub to:
Public Sub getData(ByVal uid As String, ByVal dept As String)

Code:
Public Function Get_Dept(uid_local As String) As String
    dept = "empty"
    Call getData(uid_local, dept)
    Get_Dept = dept
End Function
If I can make it work I'll change the hard-coded uid from "1012500" to a parameter that the function is called with.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Well, I know more but still have the problem.
The line:
Code:
Set wBook = Workbooks.Open("C:\Users\myuser\Documents\REFERENCE\SAP\SAS Directory 05-2012.xlsx") ' Path to external

behaves differently when it's executed as a standalone SUB vs a SUB called by my function. When I debug step-by-step, if the SUB is called manually (I have to remove the expected input parameters and hardcode them to do this), then it works and in the LOCALS window, I see that wBook becomes a workbook....but if a function calls my SUB, the wBook shows "Nothing" in the LOCALS window, so obviously, the SUB doesn't work right....now why?
 
Upvote 0
Are you sure it's the sub not working?

There are certains things a UDF can't fo and I think one of them might be open a workbook.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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