Confused on how arrays work, please help

mrlemmer11

New Member
Joined
Jun 8, 2015
Messages
32
Hello,

So, I know this is prob really simple to a lot of you, and I wish I had your mindset, but I am just not grasping this concept :(

Everytime I try to work with these I hit type mismatch or subscript mismatch errors... then on the few random occasions I change something and avoid all of those, I run into other errors when I try adding to my array. Here is what I have thus far:

Code:
Public Sub record(ByVal strShape As String, ByVal strColor As String, addOrDelete As Boolean)If addOrDelete = True Then
    ReDim Preserve allAns(0 To a, 0 To 1)
    allAns(a, 0) = strShape
    allAns(a, 1) = strColor
    a = a + 1
Else
    If addOrDelete = False Then
         a = a - 1

    Else
    End If
End If
End Sub

This sub is supposed to keep track of my users submission for strShape and strColor. For the sake this example, lets say each time a user is presented with the question "Name a shape and it's color" then the user's shape response will by strShape and the color would be strColor. Then as they progress, they can go backwards one answer at a time to reanswer a previous question. example

allAns(0,0) = square : allAns(0,1) blue
allAns(1,0) = circle : allAns(1,1) green
allAns(2,0) = triangle : allAns(2,1) green
allAns(3,0) = oval : allAns(3,1) red

then the user realizes that they didn't want to name a circle, i want the sub to then delete one at a time the answers that led up to that question so:

allAns(3,0) = deleted : allAns(3,1) deleted

... other code runs and the i call to have allAns(2,0) and allAns(2,1) deleted

allAns(2,0) = deleted : allAns(2,1) deleted

.... other code runs and i call to have allAns(1,0) and allAns(1,1) deleted

allAns(1,0) = deleted : allAns(1,1) deleted

.....other code runs and i ask the user for a shape and color again, they respond:

allAns(1,0) = hexagon : allAns(1,1) = teal

they exit the program and at that point when i look at allAns I see:

allAns(0,0) = square : allAns(0,1) blue
allAns(1,0) = hexagon : allAns(1,1) = teal

Alrighty, so that's what I see in my head, but I can't make the code I provided above in that Sub act in this fashion.... the calls I make are the following to add:

Code:
Call record("square", "blue", True)

and this to delete

Code:
Call record("triangle", "green", False)

Thank you in advance for any explanations on what concepts I just am not grasping :/
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
If you use Redim Preserve, as opposed to just Redim, you can only resize the upper boundary of the last dimension of your array, which is why your code won't work.

Since you appear to be trying to track key/value pairs, I suggest you look at using a Collection or Dictionary instead.
 
Upvote 0
I took your suggestion of looking into Collection and Dictionary and I'm just not understanding that either.... Collection has me building multiple classes and all sorts of items that I can't get to work either. :/

There has got to be a simpler way that I'm just not getting. *sigh*
 
Upvote 0
Using dictionary:

Code:
strShape = "Circle"
strColor = "Blue"
Set oShapesDictionary = CreateObject("Scripting.Dictionary")
'To add a key/value pair
oShapesDictionary.Add strShape, strColor
' to retrieve value of a key
Debug.Print oShapesDictionary.Item(strShape)
' to delete a key/value pair
oShapesDictionary.Remove strShape
 
Upvote 0
Using dictionary:

Code:
strShape = "Circle"
strColor = "Blue"
Set oShapesDictionary = CreateObject("Scripting.Dictionary")
'To add a key/value pair
oShapesDictionary.Add strShape, strColor
' to retrieve value of a key
Debug.Print oShapesDictionary.Item(strShape)
' to delete a key/value pair
oShapesDictionary.Remove strShape


So, I'm using this now and I'm running into Run-Time Error '457': This key is already associated with an element of this collection

which is why I believed I wanted to use arrays, because from my understanding, allAns(82,0) could careless that it along with allAns(16,0) aand allAns(53,0) all are ovals. Nor does it care what color they are. From my understanding, they just record what ever they are told to record, which is important to me.

For other parts of this program, I essentially need to keep track of what shape and the color that shape is which the user inputs. I also need to track this information in the order in which they told the program about it. Lastly, if they are answering the shape and color questions for lets say the 15th time, and they decide they want to change their 8th answer then I want to delete question 14, do some code, delete 13 do some code etc etc down to 8 and reask the question.... I am not looking to have the user CHANGE answer 8 and keep 9-14, they should all be deleted....

I hope this helps...
 
Upvote 0
How about this - key is an incrementing index and a shape, color array is stored as value of that dictionary key.

Code:
    Set oShapesDictionary = CreateObject("Scripting.Dictionary")
    'To add a key/value pair
    oShapesDictionary.Add 1, Array("Oval", "Blue")
    oShapesDictionary.Add 2, Array("Oval", "Green")
    oShapesDictionary.Add 3, Array("Oval", "Red")
    oShapesDictionary.Add 4, Array("Square", "Blue")
    ' to retrieve value of a key
    Debug.Print oShapesDictionary.Item(1)(0) & " => "; oShapesDictionary.Item(1)(1)
    Debug.Print oShapesDictionary.Item(2)(0) & " => "; oShapesDictionary.Item(2)(1)
    ' to delete a key/value pair
    oShapesDictionary.Remove 1
 
Upvote 0
How about this - key is an incrementing index and a shape, color array is stored as value of that dictionary key.

Code:
    Set oShapesDictionary = CreateObject("Scripting.Dictionary")
    'To add a key/value pair
    oShapesDictionary.Add 1, Array("Oval", "Blue")
    oShapesDictionary.Add 2, Array("Oval", "Green")
    oShapesDictionary.Add 3, Array("Oval", "Red")
    oShapesDictionary.Add 4, Array("Square", "Blue")
    ' to retrieve value of a key
    Debug.Print oShapesDictionary.Item(1)(0) & " => "; oShapesDictionary.Item(1)(1)
    Debug.Print oShapesDictionary.Item(2)(0) & " => "; oShapesDictionary.Item(2)(1)
    ' to delete a key/value pair
    oShapesDictionary.Remove 1

Thank you good Sir, thank you!

I was able to make this work and I believe it's exactly what I need!
 
Upvote 0

Forum statistics

Threads
1,216,584
Messages
6,131,564
Members
449,655
Latest member
Anil K Sonawane

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top