Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate over an array and populate some cells with 1 if the array contains the value, and 0 if not ,in VBA or Excel?

I'm receiving from Wix an array with the courses selected by an user via a form.

Example of an array receive from Wix where an user selected 2 courses. This array is placed in a cell.

["VBA","Django"]

This above array is place inside a cell in my excel file from where I red the cell and populate a VBA array with the courses:

Dim data() As Variant
    Dim arrStr As String
    Dim dataItems As Long
    Dim i As Long
    Dim IndividualCourses(1 To 9) As String

    arrStr = Range("T3")
    arrStr = Replace(Replace(arrStr, "[", "{"), "]", "}")
    data = Application.Evaluate(arrStr)
    dataItems = Application.CountA(data)
    For i = 1 To dataItems
        IndividualCourses(i) = data(i)
    Next i

But the user can select between 1 and 10 courses. The array for all courses:

["JS","Python","VBA","Java","Spring","C++","C#",".NET","Django","CSS"]

I want, based on the array received from Wix, to put 1 in the cell of the courses present in the array and 0 in the other courses.

For example, this courses selected ["VBA","Django"], it should put 1 in the VBA cell and 1 in the Django cell, and 0 in the remianing courses cells, because I want to see what courses an user has selected.

Any ideea for this complicated problem ?!?

like image 364
byte is 8bits Avatar asked Feb 24 '21 16:02

byte is 8bits


People also ask

How do you loop an array in Excel?

In VBA, to loop through an array you can use the For Loop (For Next). And to write this loop code you need to know the size of the array (upper and lower bound) so that you can use both as the counter for the loop. Basically, for loop will start from the first element of the array and loop up to the last.

How do you check if a value is in an array in Excel VBA?

This example uses the IsArray function to check if a variable is an array. Dim MyArray(1 To 5) As Integer, YourArray, MyCheck ' Declare array variables. YourArray = Array(1, 2, 3) ' Use Array function. MyCheck = IsArray(MyArray) ' Returns True.

How do you loop through a cell in a range?

One way to loop through a range is to use the For... Next loop with the Cells property. Using the Cells property, you can substitute the loop counter (or other variables or expressions) for the cell index numbers. In the following example, the variable counter is substituted for the row index.

How to iterate over an array in Java?

Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.

How to loop through arrays in VBA?

This tutorial will teach you how to loop through Arrays in VBA. For Each Loop – The For Each Loop will loop through each item in the array. For Next Loop – The For Next Loop will loop through specified start and end positions of the array (We can use the UBound and LBound Functions to loop through the entire array).

How to iterate through a NumPy array in Python?

Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. If we iterate on a 1-D array it will go through each element one by one. In a 2-D array it will go through all the rows. If we iterate on a n -D array it will go through n-1th dimension one by one.

How do I view the result of an array in Excel?

When working with array formulas in Excel, you can observe how they calculate and store their items (internal arrays) to display the final result you see in a cell. To do this, select one or several arguments within a function's parentheses, and then press the F9 key. To exit the formula evaluation mode, press the Esc key.


1 Answers

Not sure how set you are on VBA, but depending on your version of Excel you can do this with dynamic arrays, for example:

enter image description here

Formula in B2:

=--ISNUMBER(FIND(""""&B1:K1&"""",A2))

EDIT:

As per @T.M. his valuable comment you can feed the 2nd parameter in FIND() a whole vertical range too:

enter image description here

Formula in B2:

=--ISNUMBER(FIND(""&B1:K1&"",A2:A4))
like image 193
JvdV Avatar answered Oct 16 '22 09:10

JvdV