Not sure about how to limit that but you can create a matrix based off the tables to generate the data you are needing.
Dim m1(1), m2(3), m3(9), r1(27) as string or () if variable to set later
dim i1, i2, i3, rn as integer
'set the m1, m2, m3 values. can be set from table
rn = 1
for I1=...