UDF Custom function works in immediate window but not worksheet cell

LeroyWork

New Member
Joined
Dec 12, 2019
Messages
4
Office Version
  1. 2016
Hi All,

I have written this UDF which returns the value 2 as expected. (formula in cell "=TEST1(V2)")
1576457396348.png

Function TEST1(A)
TEST1 = Application.VLookup(A, Sheets("pivots").Range("v2:w60"), 2, 0)
End Function

However when I use the function "vlookupGANum2(A As String)" the immediate window returns the correct result but not the cell. L The cell returns zero (0)
1576457579241.png


Function vlookupGANum2(A As String).
Dim GANum2 As Integer
Dim pT2 As PivotTable

Set pT2 = Sheets("pivots").PivotTables("GA")

GANum2 = Application.VLookup(A, pT2.TableRange1.Offset(1, 0).Resize(pT2.TableRange1.Rows.Count - 1), 2, 0)
Debug.Print GANum2

End Function

Am I doing something wrong?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Your function name doesn't match when you are returning the result.

VBA Code:
Function vlookupGANum2(A As String).
Dim GANum2 As Integer
Dim pT2 As PivotTable

Set pT2 = Sheets("pivots").PivotTables("GA")

vlookupGANum2 = Application.VLookup(A, pT2.TableRange1.Offset(1, 0).Resize(pT2.TableRange1.Rows.Count - 1), 2, 0)

End Function
 
Upvote 0
Your function name doesn't match when you are returning the result.

VBA Code:
Function vlookupGANum2(A As String).
Dim GANum2 As Integer
Dim pT2 As PivotTable

Set pT2 = Sheets("pivots").PivotTables("GA")

vlookupGANum2 = Application.VLookup(A, pT2.TableRange1.Offset(1, 0).Resize(pT2.TableRange1.Rows.Count - 1), 2, 0)

End Function
Thanks. So simple. I feel a little embarassed for not picking it up. thanks again :)
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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