Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:03:22+00:00 2026-05-28T00:03:22+00:00

I need to sum over a subset of elements in an array of doubles.

  • 0

I need to sum over a subset of elements in an array of doubles. As a simple example
consider an array of 10 elements:

  Dim Array(1 To 10) As Double 

where each element has been assigned some value. Is there a function in VB that will sum the first 5 elements? Obviously this can be solved using a loop but is there a one line solution? I would like to keep my code consise. I have tried using SUM but without success.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:03:23+00:00Added an answer on May 28, 2026 at 12:03 am

    No, there is no built in summing function in VB6 that does what you want, but you can write one easily:

    Public Function SumRange(ByRef dblArray() As Double, Optional ByVal StartRange As Long = -1, Optional ByVal Length As Long = -1)
    
       '-1 on StartRange indicates start at low bound
       If StartRange = -1 Then
          StartRange = LBound(dblArray)
       End If
    
       '-1 on Length indicates to span all elements to the end of the array
       If Length = -1 Then
          Length = UBound(dblArray) - StartRange - 1
       End If
    
       Dim dTotal As Double
       Dim lNdx As Long
    
       For lNdx = StartRange To StartRange + Length - 1
          dTotal = dTotal + dblArray(lNdx)
       Next lNdx
    
       SumRange = dTotal
    
    End Function
    

    Please be aware I did not sytaz check this, so watch for typos. It can be used as follows:

    dResult = SumRange(myArray) 'summ all
    
    dResult = SumRange(myArray, 3) 'sum all starting at offset 3
    
    dResult = SumRange(myArray, , 2) 'sum the the first 2 elements
    
    dResult = SumRange(myArray, 2, 4) 'sum a range start at 2 and with length of 4
    

    You could modify the method to take another optional boolean to specifies if you will tolerate (ignore) out-of-bounds. Right now an error would be reaised if you asked to sum more elements than exist in the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use a where clause within an over clause. How? SELECT SUM(amount)
I have one field that I need to sum lets say named items However
I need sphinx to sort the results by the sum of an attribute. I
I need to write a non-recursive version of the function sum-squares and Use a
I've a need to add method that will calculate a weighted sum of worker
Given the XML below, I need to group by InputItem and sum the costs
I need some help over here to understand how the model relationship works on
I am currently trying to do a simple mapreduce over some documents stored in
select tt.threshold_id from (select sum(amount) over (partition by tt.threshold_type order by tt.threshold_type ) amt
WITH abc AS ( SELECT p_id , t1.amount as amount , SUM(amount) OVER (PARTITION

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.