VBA array from table based on criteria

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
106
Office Version
  1. 2019
  2. 2016
Hi all,

I have a structured excel table where I need to pass a reference number into an array based on whether that reference is in use or not. The table is structured as follows:

ID NumberListColumn(2)ListColumn(3)Status
1In Use
2In Use
3Free
4In Use
5 etcFree

So in this example I would want to pass ID numbers 1,2 and 4 into the array based on them being 'In Use'. Any ideas on how I would go about the section of the code dealing with the array?

Many Thanks
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
How about
VBA Code:
Sub ldarley()
   Dim Ary As Variant
   
   With ActiveSheet.ListObjects("Table1")
      Ary = Filter(Evaluate("transpose(if(" & .ListColumns("Status").DataBodyRange.Address & "=""In Use""," & .ListColumns("ID Number").DataBodyRange.Address & ",false))"), False, False)
   End With
End Sub
 
Upvote 0
Another possibility

VBA Code:
Sub Into_Array()
  Dim Ary As Variant
 
  Ary = Application.Index(Range("Table1[ID Number]").Value, Filter(Application.Transpose(Evaluate(Replace("if(#=""In Use"",Row(#)-" & Range("Table1[#Headers]").Row & ",""x"")", "#", Range("Table1[Status]").Address))), "x", False), 1)
End Sub

There are a couple of slight differences between my results & Fluffs.
- If the ID numbers are in fact numerical like your sample, my code will populate the array with numbers whereas Fluff;s will populate with text.
- My array will be 1-based, Fluff's will be zero-based
 
Upvote 0
Solution
Thanks Peter and Fluff I've tried them out and they both work great, Peter I've leant towards your for the reason that the data is numerical.

Peter for my own learning exactly what is your code doing in the section related to the table headers is that because the index is looking at the whole column including the header and not just the databodyrange (or am I way off!)?
 
Upvote 0
Peter for my own learning exactly what is your code doing in the section related to the table headers ...
The Evaluate part of my code (which is the part that uses [#Headers]) is to produce an array of row numbers for "In Use" rows. However, I want those row numbers to be relative to the DataBodyRange. In the example below the worksheet row numbers for the relevant rows are {4,5,7}. If we subtract the worksheet row number of the Headers row (3) that array becomes {1,2,4}

So the array is populated by indexing the [ID Numbers] column for rows {1,2,4} and column 1, feeding the required ID Numbers into the array.
Hope that makes sense. :)
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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