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
Solved

For statement algorithm for VBA

Posted on 2011-09-24
2
228 Views
Last Modified: 2012-08-14

c array size is  x
b array size is x2

I have the following table for example.

c(1) = b(1)                  
c(2)=((b(2)+1)*(c(1)+1))-1      c(6)=b(2)            
c(3)=)(b(3)+1)*(c(2)+1))-1      c(7)=((b(3)+1)*c(6)+1))-1      c(10)=b(3)      
c(4)=)(b(4)+1)*(c(3)+1))-1      c(8)=((b(4)+1)*c(7)+1))-1      c(11)=((b(4)+1)*c(10)+1))-1      
c(5)=)(b(5)+1)*(c(4)+1))-1      c(9)=((b(5)+1)*c(8)+1))-1      c(12)=((b(5)+1)*c(11)+1))-1      

How would i put these in a for statement? Again C goes until x, B goes until x2.

0
Comment
Question by:awesomejohn19
2 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 36593040
I don't know does thismeet the requirement ?
gowflow
Sub ForStatement()

Dim I As Long, J As Long, X As Long
Dim B(X), C(2 * X)


For I = 1 To X
    For J = 0 To 4
        C(I) = (B(I) + 1) * (C(J) + 1) - 1
    Next J
Next I

End Sub

Open in new window

0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 36593211
Here's your solution - note I've initialized x2 as a constant = 5 per your example, which can be changed based on how you're using this in your algorithm.  x must be some factor larger than x2, example if x2 = 5, x must be 12, if x2 = 10, x must be  52, etc.  As a result, I redimensioned c based on that fact

 
Const x2 = 5
'Const x = 12 'must be large enough to get through all the iterations... e.g., if x2 = 5, then x must be 12, if x2 = 10 then x must be 52, etc.
'as array c() is a function of array b(), it stand to reason that c() can be dynamic and allocate what it needs on the fly, thus the declaration,
'and the redim preserve statement
Sub forStmt()

Dim i As Long, j As Long, k As Long
Dim b(1 To x2) As Variant, c() As Variant

        
    k = 1
    For i = 1 To x2 - 2
        For j = i To x2
            ReDim Preserve c(k) As Variant
            
            c(k) = IIf(j = i, b(j), (b(j) + 1) * (c(k - 1) + 1) - 1)

            k = k + 1
            
        Next j
    Next i

End Sub

Open in new window


For fun, see attached workbook - enter any value for x2 > 3 and you'll see your table of formulas.  This demonstrates that what I have given you works exactly as you've specified.

Cheers,

Dave
genFormula-r1.xlsm
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

856 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