Solved

MS Excel - working with Arrays and looping through code

Posted on 2006-07-18
4
185 Views
Last Modified: 2010-04-30
I am trying to create an array from two values. For example, lets say cell A1 = 5 and B1 = 7. I want code that examines the two values, identifies the smallest, creates an array that counts from the smallest down to 0, while increasing the largest at the same time:

5   7
4   8
3   9
2   10
1   11
0   12

My code is not working, and I am coming up with some funny values. What am I doing wrong?  Here is my code:

Function createArray(cellA, cellB)

Dim MPos1, getMin, myNewArray(100, 2), rw, prw
Dim myArray(100, 2)

    If cellA <= cellB Then
        MPos1 = 1
        getMin = cellA
    Else
        MPos1 = 2
        getMin = cellB
    End If
   
myNewArray(0, 1) = cellA
myNewArray(0, 2) = cellB

For rw = 1 To getMin

    prw = rw - 1

    If MPos1 = 1 Then myNewArray(rw, 1) = myNewArray(prw, 1) - 1 Else myNewArray(rw, 1) = myNewArray(prw, 1) + 1
    If MPos1 = 1 Then myNewArray(rw, 2) = myNewArray(prw, 1) + 1 Else myNewArray(rw, 1) = myNewArray(prw, 2) - 1
    rw = rw + 1
   
Next

createArray = myNewArray(rw - 1, 2)


End Function
0
Comment
Question by:smehle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 17135286
Hi smehle,

Function createArray(cellA As Long, cellB As Long)

    Dim myArray() As Long
    Dim First As Long
    Dim Second As Long
    Dim Counter As Long

    If cellA <= cellB Then
        First = cellA
        Second = cellB
    Else
        First = cellB
        Second = cellA
    End If

    ReDim myArray(0 To First, 0 To 1) As Long

    For Counter = 0 To UBound(myArray, 1)
        myArray(Counter, 0) = First - Counter
        myArray(Counter, 1) = Second + Counter
    Next

    createArray = myArray

End Function

Regards,

Patrick
0
 

Author Comment

by:smehle
ID: 17138162
Thank you both, this was driving me NUTS!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17138247
You're welcome :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question