JONABRA
New Member
- Joined
- Mar 15, 2019
- Messages
- 19
Hi all. I have a spreadsheet with x number of columns, I'm looking for the string "YES" in each row of a specified column and once found I place the name linked to the fruit into a jagged array. For example I have a name, surname and fruit columns. The reason I would like a jagged array is that I would like to use less code, like maybe 1 or 2 nested for loops to populate the array. Also I don't want to have any empty spaces in the array, which I believe jagged arrays overcome.
here is an example of what I would like in the jagged arrays of varying lengths, fruit is the array name:
fruit(1)("Herschelle Gibbs","Dale Abrahams","Dale Steyn","Sipho Masiko")
fruit(2)("Dale Abrahams","Sipho Masiko")
fruit(3)("Herschelle Gibbs"."Sipho Masiko")
I know the following bit of code will add the linked name and surname of only one fruit columns into an array but how do I achieve this over multiple columns, with varying lengths, using a few for loops perhaps?
Much thanks in advance, and I hope this explanation makes sense.
A | B | C | D | E | |
---|---|---|---|---|---|
1 | name | surname | apple fruit(1) | banana fruit(2) | pear fruit(3) |
2 | Herschelle | Gibbs | YES | YES | |
3 | Dale | Abrahams | YES | YES | |
4 | Dale | Steyn | YES | ||
5 | Sipho | Masiko | YES | YES | YES |
here is an example of what I would like in the jagged arrays of varying lengths, fruit is the array name:
fruit(1)("Herschelle Gibbs","Dale Abrahams","Dale Steyn","Sipho Masiko")
fruit(2)("Dale Abrahams","Sipho Masiko")
fruit(3)("Herschelle Gibbs"."Sipho Masiko")
I know the following bit of code will add the linked name and surname of only one fruit columns into an array but how do I achieve this over multiple columns, with varying lengths, using a few for loops perhaps?
VBA Code:
Dim name, surname, foundyes(), namesurname() as variant
'I got lrow using the usual method of rows.count etc
'i use countyes to dimension the length of the array
countyes = Application.WorksheetFunction.countIF(sheet1.Range("c1:c" & lrow), "YES")
'there are two columns in foundyes one for the surname and one for the name, namesurname is to combine them together
ReDim foundyes(1 To 2, 1 To countyes)
ReDim namesurname(1 To countyes)
counter =1
For i = 2 To lrow
If Range("c" & i) = "YES" Then
foundyes(1, counter) = Range("a" & i).Value
foundyes(2, counter) = Range("b" & i).Value
name = foundyes(1, counter)
surname = foundyes(2, counter)
namesurname(counter) = name & " " & surname
counter = counter + 1
End If
Next i
Much thanks in advance, and I hope this explanation makes sense.