0

I am having trouble with looping through my already defined array to find values that are over 500. It is telling me that my subscript is out of range.

This is what I have so far. Can someone help me to fix the second part, so it will properly loop through the already established arrays.

Sub hishSale()

    Dim customers() As String
    Dim sale() As String
    Dim hiCustomer() As String
    Dim hiSale() As String
    Dim nCustomers As Integer
    Dim i As Integer
    Dim isOver As Integer


    'capture the number of customers and sales and put them in two seperate arrays
    With wsData.Range("A3")
        nCustomers = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
        ReDim customers(nCustomers)
        ReDim sale(nCustomers)
        For i = 1 To nCustomers
            customers(i) = .Offset(i, 0).Value
            sale(i) = .Offset(i, 1).Value
        Next
    End With

'Loop through arrays to find sales over $500

    With Range("A3")
    isOver = 0
        For i = 1 To nCustomers
            If .Offset(i, 1).Value > 500 Then
                isOver = isOver + 1
                hiCustomer(isOver) = .Offset(i, 0).Value
                hiSale(isOver) = .Offset(isOver, 1).Value
            End If
        Next
    End With

    'return results in columns D and E
    With Range("D3")
        For i = 1 To nCustomers
        .Offset(i, 0) = hiCustomer(isOver)
        .Offset(i, 1) = hiSale(isOver)
        Next
    End With

End Sub
5
  • To loop array do for lbound(customers()) to ubound(customers()) Commented May 26, 2016 at 19:51
  • In your final loop maybe you meant to use i to index into hiCustomer and hiSale ? You're using isOver, the value of which is fixed at this point Commented May 26, 2016 at 20:35
  • You need to indicate exactly which line has the error... Commented May 26, 2016 at 20:37
  • Seems like you didn't like any of these answers. What was wrong with them? This time are you going to give some feedback to pepole who are willing to dedicate time to problems you throw in here? Commented May 26, 2016 at 20:57
  • The line that has a problem is: With Range("D3") For i = 1 To nCustomers .Offset(i, 0) = hiCustomer(isOver) .Offset(i, 1) = hiSale(isOver) Next End With .Offset(i, 0) = hiCustomer(isOver) --- is giving me an error Commented May 26, 2016 at 21:03

1 Answer 1

1

I started to write a big description an walk through about your arrays being bigger then needed and how they have a base of zero, while doing so I found the problem was you never dimension hiCustomer OR hiSale.

Change this: -

'Loop through arrays to find sales over $500
With Range("A3")
isOver = 0
    For i = 1 To nCustomers
        If .Offset(i, 1).Value > 500 Then
            isOver = isOver + 1
            hiCustomer(isOver) = .Offset(i, 0).Value
            hiSale(isOver) = .Offset(isOver, 1).Value
        End If
    Next
End With

To this: -

ReDim hiCustomer(0)
ReDim hiSale(0)
With Range("A3")
    For i = 1 To nCustomers
        If .Offset(i, 1).Value > 500 Then
            ReDim Preserve hiCustomer(UBound(hiCustomer, 1) + 1)
            ReDim Preserve hiSale(UBound(hiSale, 1) + 1)
            hiCustomer(UBound(hiCustomer, 1)) = .Offset(i, 0).Value
            hiSale(UBound(hiCustomer, 1)) = .Offset(i, 1).Value
        End If
    Next
End With

And if I am reading it correct the next loop will also need changing to: -

'return results in columns D and E
With Range("D3")
    For i = 1 To UBound(hiCustomer)
        .Offset(i, 0) = hiCustomer(i)
        .Offset(i, 1) = hiSale(i)
    Next
End With

Previously it was looking the customer list and not just the >500 list.

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

2 Comments

Thank you! This works. Is there a way you can write the code without using Ubound?
@th65 yes but it would be longer. Add it is a new question and I'm sure someone will help. I'm curious as to the why.

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.