Accessing a 4 dimension array after it has been 'loaded' in another procedure

gkis2

New Member
Joined
Dec 5, 2018
Messages
17
I have declared a 4 D array then 'filled' it with some data. I know enough about arrays to get a value out of it e.g. MsgBox "output " & FourDarr(2,1,2,3). This works as long as I am in the same Sub. Maybe I should be just satisfied that I got this far. But the point of making this monstrous array is that I could then, at any time and in any module, simply come up with the 'coordinates' (by this I mean the '2,1,2,3' that I used in my earlier example) then call up the value stored in the array. I am using r,h,i and s for the four values. So maybe I would say ws.Range("R7") = FourDarr(4,h,7,6) and the value stored in that position in the array would appear in R7. Obvs I am simplifying what I would actually do so as to make the point simply. I have tried declaring the array as a Public variable. I have tried calling it in a sub but I don't like that and it didn't work. The reason I don't like it is my understanding(which could be wrong) that that would run the process of loading the array each time its called and that would defeat my purpose of putting that process up front so the wait time is only slightly annoying once rather than each time it is called. At least up to this point in development, I don't have any plans to modify the values in the array as I use them in the various modules and subs and would only need to read out the value stored. Not sure if that fact would change the best answer. I could just read the original data off of a sheet where it comes from but my understanding (again subject to its own inaccuracies) using an Array should be much faster and would certainly be less cumbersome code wise. I have searched around and found lots of articles , blogs on the basics on how to use an array and a few mentioned the Public variable and other ideas but my brain is not getting around this one. All help is much appreciated.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
VBA Code:
Option Explicit
 Public r, h, i, s As Integer

 Public FourDarr(5, 5, 5, 5) As Variant

' Loads the array with Data
Public Sub FourDarray()
Dim ws As Worksheet
Set ws = Sheet1

Dim rngData As Range
Dim rngRace As Range
Dim rngMap As Range
Dim rw, cl As Integer
' Dim FourDarr(5, 5, 5, 5) As String
' initalize to top left corner of data
Set rngData = ws.Range("O6")
Set rngRace = ws.Range("A6")
Set rngMap = ws.Range("F6")
Dim ri, hi, ii, si As Integer
' From number of races in B4
ri = ws.Range("B4")
' Max of items in rngMap - actually should be fixed
ii = Application.WorksheetFunction.Max(ws.Range("F7:F1000"))
rw = 0
cl = 0
For r = 1 To ri
rw = rngRace.Offset(r, 1)
rngData.Offset(rw, cl).Select
For h = 1 To rngRace.Offset(r, 3)
rw = rw + 1
rngData.Offset(rw, cl).Select
For i = 1 To ii
cl = rngMap.Offset(i, 3)
rngData.Offset(rw, cl).Select
For s = 1 To rngMap.Offset(i, 2)
cl = cl + 1
rngData.Offset(rw, cl).Select
FourDarr(r, h, i, s) = rngData.Offset(rw, cl)
Debug.Print "race " & r & " horse " & h & _
" item " & i & " serial " & s & " Data " & _
FourDarr(r, h, i, s)

Next s
Next i
Next h
Next r
MsgBox "OK, now we are done"
MsgBox FourDarr(2, 2, 2, 1)

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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