0

I want to add cell values from two columns into array. I can do it for one column values using:

            ss = Range("B1:R" & lstrow).Value
            For i = 2 To lstrow
            If ss(i, 2) = "FLAG" Then 
            If IsEmpty(dd) Then
            dd(i) = Array(ss(i, 1))
             Else
           ReDim Preserve dd(UBound(dd) + 1)
           dd(UBound(dd)) = ss(i, 1)
                            End If

What If I want to add to dd array another column value for example ss(i,3) so IF ss(i,2) = flag add values of ss(i,1) AND ss(i,3) to dd array

Appreciate your help

2 Answers 2

1

Use

Redim Preserve dd(1 To UBound(dd) + 1, 1 To 3)

This provides 3 columns for every array row.

Sign up to request clarification or add additional context in comments.

3 Comments

I get EXPECTED: LIST SEPERATOR OR)
There's some things I can't see whats going on with your code, such as what lstrow is, or what happens after end if. But the main point of my answer is that you can Define a variant multi-dimensional array using "Dim myArray(1 to 3, 1 to 3) as Variant "
UBound(myArray, 1) will give you the row upper bound, UBound(myArray, 2) will give you the column upper bound.
0

Looks like you want to modify your code to include values from both the second and third columns when ss(i, 2) = "FLAG". You can simply update your array creation logic inside the loop to include both values. Here's the modified code:

ss = Range("B1:R" & lstrow).Value

For i = 2 To lstrow
    If ss(i, 2) = "FLAG" Then 
        If IsEmpty(dd) Then
            ' If dd is empty, create a new array with both values
            dd(i) = Array(ss(i, 1), ss(i, 3))
        Else
            ' If dd is not empty, append a new array with both values
            ReDim Preserve dd(UBound(dd) + 1)
            dd(UBound(dd)) = Array(ss(i, 1), ss(i, 3))
        End If
    End If
Next i

This modification creates an array with both ss(i, 1) and ss(i, 3) values when ss(i, 2) = "FLAG".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.