Help with UDF Array

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
in the following code, I am trying to Extract just the "Y" numbers. My code gives me the answer I need, but the array UDF uses 2 columns. I only want my answer to be in column E not column F. What am I doing wrong?
2020-01-20_20-26-49.png


VBA Code:
Option Explicit
Function getY(rng As Range)
Dim i As Integer, j As Integer, n As Integer
Dim temp As Integer, A()
n = rng.Rows.Count
ReDim A(n, 1)
A = rng
    For j = 1 To n
        If A(j, 2) = "Y" Then
            temp = A(j, 1)
            A(j, 2) = ""
        Else
            A(j, 1) = ""
            A(j, 2) = ""
        End If
    Next j
getY = A
End Function
 
If you want to stick to VBA, how about
VBA Code:
Function getY(Rng As Range) As Variant
   Dim j As Long, n As Long
   Dim A As Variant, B As Variant
   n = Rng.Rows.Count
   ReDim B(1 To n, 1 To 1)
   A = Rng.Value2
   For j = 1 To n
      If A(j, 2) = "Y" Then
         B(j, 1) = A(j, 1)
      Else
         B(j, 1) = ""
      End If
   Next j
   getY = B
End Function
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Here is another function that you could try

VBA Code:
Function getY(rng As Range)
  getY = Evaluate(Replace(Replace("if(#=""Y"",^,"""")", "#", rng.Columns(2).Address), "^", rng.Columns(1).Address))
End Function


I'm assuming due to :)
thank you!
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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