0

This is making me nuts. I am trying to extract the values of columns that fit my criteria (TRUE or 1) from a user-selected row , save only those to an array, and then write the array to a range. That's all. Can anyone tell me what I'm doing wrong?

Sub DevNeeds()
Dim x(), y(), needs() As Variant
Dim counter As Integer

columns_in_range = Range("dev_needs_hdrs").Columns.Count
counter = 1

Debug.Print "i", "counter", "y(counter)"

For i = 1 To columns_in_range
    ReDim x(columns_in_range), needs(columns_in_range)
    x(i) = Application.Index(Range("dev_needs"), Range("selected_row").Value, i)
    needs(i) = Application.Index(Range("dev_needs_hdrs"), 1, i)

    If (x(i) = True Or x(i) = 1) Then
        ReDim y(counter)
        y(counter) = needs(i)
        counter = counter + 1
    End If
Next i
counter = counter - 1

With Range("selected_rep_needs")
    .ClearContents
    .Resize(1, counter) = y
End With

End Sub
5
  • What line receives the error? Commented Sep 2, 2014 at 19:42
  • actually, i don't get an error. i just get unexpected results, i.e., the range "selected_rep_needs" does not get anything written to it. also, the array named "y" doesn't hold any values except for the last subscript (last value of "counter") Commented Sep 2, 2014 at 19:56
  • I don't suppose the range name argument in Application.Index that assigns values to x(i) should be "dev_needs_hdrs", not just "dev_needs"(?). Commented Sep 2, 2014 at 20:04
  • 1
    Also, it looks like you need ReDim Preserve or your array values will be overwritten. Commented Sep 2, 2014 at 20:07
  • thanks matt! actually "dev_needs_hdrs" is correct. i added that rather than redefining the "dev_needs" range to include the header row. probably bad programming. i didn't know about ReDim Preserve. I'm going to Google that now. Commented Sep 2, 2014 at 20:17

1 Answer 1

2

Also, it looks like you need ReDim Preserve or your array values will be overwritten. – Matt Cremeens

THIS WAS THE PROBLEM. Thanks Matt. Wish I could give you credit for the answer, but you posted it as a comment! -- SteveS.

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

2 Comments

You should also accept your own answer, as this will encourage other people to look into this question and learn as well :)
I will do that, as soon as I'm allowed to. It's a 3-day wait to OK your own answer. Thanks for looking at the thread, though. I appreciate the quick help I got on here!

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.