+ Reply to Thread
Results 1 to 3 of 3

SumProduct with variable array

  1. #1
    Lynn
    Guest

    SumProduct with variable array

    I need to calculate the sum of A1*A10 on all sheets in a workbook except for
    Sheet 1 - ie Sheet2!a1*Sheet2!a10 + Sheet3!a1*Sheet3!a10 etc. I currently
    have 4 sheets in the workbook + my code looks like this:

    A = Array(Sheets(2).Range("a1").Value, Sheets(3).Range("a1").Value,
    Sheets(4).Range("a1").Value)
    B = Array(Sheets(2).Range("a10").Value, Sheets(3).Range("a10").Value,
    Sheets(4).Range("a10").Value)
    answer = Application.WorksheetFunction.SumProduct(A, B)

    The problem is that the number of sheets in the workbook will vary over time
    - how can I incorporate ActiveWorkbook.Sheets.Count or some other code to
    have it calculate the answer for the current # of sheets in the workbook? Any
    suggestions most welcome!!

  2. #2
    Tom Ogilvy
    Guest

    Re: SumProduct with variable array

    Dim A() as Double
    Dim B() as Double
    Dim i as Long
    Dim answer as Double
    redim A(2 to worksheets.count)
    redim B(2 to worksheets.count)
    for i = 2 to worksheets.count
    A(i) = worksheets(i).Range("A1").Value
    B(i) = worksheets(i).Range("A10").Value
    Next
    answer = Application.SumProduct(A,B)

    --
    Regards,
    Tom Ogilvy


    "Lynn" <[email protected]> wrote in message
    news:[email protected]...
    > I need to calculate the sum of A1*A10 on all sheets in a workbook except

    for
    > Sheet 1 - ie Sheet2!a1*Sheet2!a10 + Sheet3!a1*Sheet3!a10 etc. I currently
    > have 4 sheets in the workbook + my code looks like this:
    >
    > A = Array(Sheets(2).Range("a1").Value, Sheets(3).Range("a1").Value,
    > Sheets(4).Range("a1").Value)
    > B = Array(Sheets(2).Range("a10").Value, Sheets(3).Range("a10").Value,
    > Sheets(4).Range("a10").Value)
    > answer = Application.WorksheetFunction.SumProduct(A, B)
    >
    > The problem is that the number of sheets in the workbook will vary over

    time
    > - how can I incorporate ActiveWorkbook.Sheets.Count or some other code to
    > have it calculate the answer for the current # of sheets in the workbook?

    Any
    > suggestions most welcome!!




  3. #3
    Lynn
    Guest

    Re: SumProduct with variable array

    Thanks Tom - this works great!!

    "Tom Ogilvy" wrote:

    > Dim A() as Double
    > Dim B() as Double
    > Dim i as Long
    > Dim answer as Double
    > redim A(2 to worksheets.count)
    > redim B(2 to worksheets.count)
    > for i = 2 to worksheets.count
    > A(i) = worksheets(i).Range("A1").Value
    > B(i) = worksheets(i).Range("A10").Value
    > Next
    > answer = Application.SumProduct(A,B)
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Lynn" <[email protected]> wrote in message
    > news:[email protected]...
    > > I need to calculate the sum of A1*A10 on all sheets in a workbook except

    > for
    > > Sheet 1 - ie Sheet2!a1*Sheet2!a10 + Sheet3!a1*Sheet3!a10 etc. I currently
    > > have 4 sheets in the workbook + my code looks like this:
    > >
    > > A = Array(Sheets(2).Range("a1").Value, Sheets(3).Range("a1").Value,
    > > Sheets(4).Range("a1").Value)
    > > B = Array(Sheets(2).Range("a10").Value, Sheets(3).Range("a10").Value,
    > > Sheets(4).Range("a10").Value)
    > > answer = Application.WorksheetFunction.SumProduct(A, B)
    > >
    > > The problem is that the number of sheets in the workbook will vary over

    > time
    > > - how can I incorporate ActiveWorkbook.Sheets.Count or some other code to
    > > have it calculate the answer for the current # of sheets in the workbook?

    > Any
    > > suggestions most welcome!!

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1