User Defined "Union" function to ignore "Nothing" entries

smeador

New Member
Joined
Nov 30, 2014
Messages
12
Hello,

I have a user defined function as seen below that allows me to combine two non-adjacent columns into a contiguous array.

Code:
Function MakeContig(ParamArray av() As Variant) As Variant
    Dim avOut() As Variant
    Dim i       As Long
    Dim j       As Long


    ReDim avOut(1 To av(0).Count, 0 To UBound(av))
    For j = 0 To UBound(av)
        For i = 1 To av(j).Rows.Count
            avOut(i, j) = av(j)(i)
        Next i
    Next j
    MakeContig = avOut
End Function
So, if I type "=makecontig(A1:A5,E1:E5)" into a cell, a single array is formed consisting of the two columns included in the formula. I would like to be able to edit this function so that it ignores a non-array entry.

For example, if i type "=makecontig(A1:A5,E1:E5, , )" or =makecontig(A1:A5,E1:E5,Nothing)" I would like it to ignore the error or blank parameter and only create a contiguous array for A1:A5 and E1:E5.

I imagine I need to include an If/Then statement somewhere in UDF but I cannot seem to figure out how to execute it properly.

Any help would be appreciated!
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

ask2tsp

Well-known Member
Joined
Feb 18, 2015
Messages
506
Office Version
365
Platform
Windows
Try this code. It only processes RANGE arguments, all others are ignored.
While I was at it I made it so the ranges can be of different sizes. The number of rows in the output array equals the number of cells in the largest range. Excess rows in the smaller ranges are zero filled.
Code:
Option Explicit

Function MakeContig(ParamArray av() As Variant) As Variant
    Dim avOut() As Variant
    Dim i       As Long
    Dim j       As Long
    Dim notEcnt As Long
    Dim maxRows As Long
    Dim outCol  As Long
    
    notEcnt = -1
    For i = 0 To UBound(av())
        If Not IsError(av(i)) Then
          If TypeOf av(i) Is Range Then
            notEcnt = notEcnt + 1
            If av(i).Rows.Count > maxRows Then _
                maxRows = av(i).Rows.Count
          End If
        End If
    Next i
    
    ReDim avOut(1 To maxRows, 0 To notEcnt)
    
    outCol = -1
    For j = 0 To UBound(av())
        If Not IsError(av(j)) Then
          If TypeOf av(j) Is Range Then
            outCol = outCol + 1
            For i = 1 To maxRows
                avOut(i, outCol) = av(j)(i)
            Next i
          End If
        End If
    Next j
    MakeContig = avOut
End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,102,042
Messages
5,484,355
Members
407,437
Latest member
alfaroM

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top