0

I can't figure out how to get multiple coordinates into a single dimensional array in VBA to use later. Here's the part of the code where I'm adding values to a coordinate array. I couldn't figure out how to add the x,y,z data to a single dimensional array. I've tried different methods but cannot get multiple values to show even as a string.

The goal of this code is to use coordinate data to feed into another function by passing a coordinate array. In this section of code I am acquiring the coordinate data from a sketch with the vLines function that holds 6,7,8 as x,y,z data. I want to hold all of this x,y,z data in the CoordinatesArray which I'm going to access later. Should I really use another array for the xyz data or a string? What would be a good method for accessing or should I make a 4D array with coordinate number and the three positions later?

I want the coordinates array to be

coordinate(1) = x1, y1, z1

coordinate(2) = x2, y2, z2

coordinate(n) = xn, yn, zn

For i = 0 To NumLines - 1
        coordx1 = (vLines(12 * i + 6))
        coordy1 = (vLines(12 * i + 7))
        coordz1 = (vLines(12 * i + 8))
        CoordinatesArray(i) = coordx1 & "" & coordy1
        'CoordinatesArray(i) = CStr(coordx1) & CStr(coordy1) & CStr(coordz1)
        Debug.Print "Coordinate Array = "; CoordinatesArray(i)
        Debug.Print "  Line(" & i & ")"
        Debug.Print "    Start = (" & vLines(12 * i + 6) * 1000# & "," & vLines(12 * i + 7) * 1000# & "," & vLines(12 * i + 8) * 1000# & ") mm"
        Debug.Print "    End   = (" & vLines(12 * i + 9) * 1000# & "," & vLines(12 * i + 10) * 1000# & "," & vLines(12 * i + 11) * 1000# & ") mm"
    Next i
1
  • Perhaps coordinate(1) = array(x1, y1, z1). Commented Sep 22, 2017 at 17:25

1 Answer 1

2

As a "jagged array" (an array of arrays)

For i = 0 To NumLines - 1
        coordx1 = (vLines(12 * i + 6))
        coordy1 = (vLines(12 * i + 7))
        coordz1 = (vLines(12 * i + 8))

        CoordinatesArray(i) = Array(coordx1, coordy1, coordz1)
Next i

To access:

Debug.Print CoordinatesArray(5)(0) 'x
Debug.Print CoordinatesArray(5)(1) 'y
Debug.Print CoordinatesArray(5)(2) 'z

As a 2-D array

Redim CoordinatesArray(1 to NumLines, 1 to 3) 

For i = 0 To NumLines - 1
        coordx1 = (vLines(12 * i + 6))
        coordy1 = (vLines(12 * i + 7))
        coordz1 = (vLines(12 * i + 8))

        CoordinatesArray(i, 1) = coordx1
        CoordinatesArray(i, 2) = coordy1
        CoordinatesArray(i, 3) = coordz1
Next i

To access:

Debug.Print CoordinatesArray(5, 1) 'x
Debug.Print CoordinatesArray(5, 2) 'y
Debug.Print CoordinatesArray(5, 3) 'z

Or you could create a custom Type or Class with fields x, y and z and create an array of those.

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

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.