Results 1 to 2 of 2

Thread: RESOLVED[Vector in vba]

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved RESOLVED[Vector in vba]

    Lets say I declare the following variables which are between 0 and 1.

    VB Code:
    1. Dim a As byte
    2. Dim b as byte
    3. Dim c As byte
    4. Dim d As byte
    5. '... etc
    In fact there are 37 variables.

    Then I have a list of 37 constants in some worksheet, and I put them into a vector called m.

    VB Code:
    1. Dim m() As Integer
    2. ReDim m(37)
    3. For i= 1 to 37
    4. m(i)= Cells(1,i).Value
    5. Next i

    My problem is to compute the following product:
    op=a*m1+b*m2+c*m3+...
    How can I use each of the elements of the vector m, in order to multiply them to an indiividual element (e.g. a or b or c, etc)?
    Last edited by Fonty; May 19th, 2006 at 10:07 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Vector in vba

    Moved to Office Development forum (as this is a code issue, rather than a mathematical problem)

    As you have it, you can't.

    Your a/b/c/etc.. need to be an array, just like m is. You can then use a loop to add/multiply the values together, eg:
    VB Code:
    1. Dim mul() as Byte  '(same as a/b/c/etc)
    2. Dim myTotal as Long
    3.   ReDim mul(37) as Byte
    4.   'set values for mul here
    5.  
    6.   myTotal = 0
    7.   For i = 1 to 37
    8.     myTotal = myTotal + mul(i) * m(i)
    9.   Next i
    10.  
    11.   MsgBox myTotal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width