Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 28

Thread: How do I get the total from all array elements

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location

    How do I get the total from all array elements

    Hello people,

    I have made following code it is an array that loop through certain columns and each row,

    How do I sum all all cells result to an total?

    
    
    Sub myABArr()
    
    
    
    
    Dim myStrArray As Variant
    
    
    
    
    myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
        
        For i = LBound(myStrArray) To UBound(myStrArray)
        
        myCol = myStrArray(i)
            For x = 2 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
                
            
                  '  MsgBox Cells(x, mycol).Value ' I can get any numbers example 134
      
             
            Next x
        
        Next i
    
    
    
    
    End Sub


    thank you in advance.
    Last edited by elmnas; 09-21-2015 at 02:21 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    msgbox application.sum(intersect(sheet1.usedrange,sheet1.columns(14).resize(,11)))

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    Quote Originally Posted by snb View Post
    msgbox application.sum(intersect(sheet1.usedrange,sheet1.columns(14).resize(,11)))
    Doesn't work.
    Object required.

  4. #4
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    I used instead but it still wrong result
    MsgBox Application.Sum(Intersect(ActiveSheet.UsedRange, ActiveSheet.UsedRange.Columns(14).Resize(, 11)))

  5. #5
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    Hi elmnas,
    with your code use
    Sub myABArr() 
        Dim myStrArray As Variant 
        myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y") 
        For i = LBound(myStrArray) To UBound(myStrArray) 
            myCol = myStrArray(i) 
            For x = 2 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 
               mysum =mysum+ Cells(x, mycol).Value ' I can get any numbers example 134
            Next x 
        Next i 
        msgbox mysum 
     End Sub

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't see why you needed an extra null value in your array.

    As you found out using snb's code, changing Sheet1 to ActiveSheet fixed the object issue. If you need that formula on all your sheets, ActiveSheet object is best.

    To compute the "correct" result, we can see that snb missed the exclusion of column Q. We can simply substract it. Using his method, you should also note that if you have any value in those column's first row, an "incorrect" value will be found as well.
    Sub ken()  
      With ActiveSheet
        MsgBox Application.Sum(Intersect(.UsedRange, .Columns(14).Resize(, 11))) _
          - Application.Sum(Intersect(.UsedRange, .Columns(17)))  '17=Column Q
         End With
    End Sub

  7. #7
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Drop the UsedRange in the second region of the intersect. So;

    MsgBox Application.Sum(Intersect(ActiveSheet.UsedRange, ActiveSheet.UsedRange.Columns(14).Resize(, 11)))
    becomes

    MsgBox Application.Sum(Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns(14).Resize(, 11)))
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub SamT()
    
    Dim myStrArray As Variant
    Dim Col As String
    Dim LR As Long
    Dim Result As Double
    
      myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
      LR = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
      
      For i = LBound(myStrArray) To UBound(myStrArray)
        Col = myStrArray(i)
        If Not Col = "" Then 'Handles Null Value in array
          Result = Result + Sum(Range(Col & "2" & ":" & Col & LR))
        End If
      Next
      
      MsgBox Result
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    3
    Location
    Hi

    I cannot post a new thread how do I do this?

    Thanks

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Select the forum about the application, (Excel, Word, Access, etc,) or that best fits your question, (Introductions, SQL, Project Assistance, etc.) At the bottom of the Threads list, clivk on +Post New Thread
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  11. #11
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    3
    Location
    thanks Samt! Posted the question hope one of you genius's can answer it

  12. #12
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    Quote Originally Posted by pike View Post
    Hi elmnas,
    with your code use
    Sub myABArr() 
        Dim myStrArray As Variant 
        myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y") 
        For i = LBound(myStrArray) To UBound(myStrArray) 
            myCol = myStrArray(i) 
            For x = 2 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 
               mysum =mysum+ Cells(x, mycol).Value ' I can get any numbers example 134
            Next x 
        Next i 
        msgbox mysum 
     End Sub
    incompatible types error 13

    Doesn't work.

  13. #13
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    Quote Originally Posted by SamT View Post
    Sub SamT()
    
    Dim myStrArray As Variant
    Dim Col As String
    Dim LR As Long
    Dim Result As Double
    
      myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
      LR = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
      
      For i = LBound(myStrArray) To UBound(myStrArray)
        Col = myStrArray(i)
        If Not Col = "" Then 'Handles Null Value in array
          Result = Result + Sum(Range(Col & "2" & ":" & Col & LR))
        End If
      Next
      
      MsgBox Result
    End Sub
    sub or the function is not defined.

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please Elmnas, do something yourself: testing, analysing, varying something in the code.

  15. #15
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    Hello elmnas,
    Not knowing the content of the cells
    Option Explicit
    Sub Test()
        Dim myStrArray As Variant
        Dim Col As String
        Dim xRow As Long
        Dim Result As Double
        Dim iCount As Long
        
        myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
        For iCount = LBound(myStrArray) To UBound(myStrArray)
            If Not IsError(myStrArray(iCount)) Then
                For xRow = 2 To ActiveSheet.Cells(Rows.Count, myStrArray(iCount)).End(xlUp).Row
                    Result = Result + IIf(IsNumeric(Cells(xRow, myStrArray(iCount)).Value), Cells(xRow, myStrArray(iCount)).Value, 0)
                Next xRow
            End If
        Next iCount
        MsgBox Result
    End Sub

  16. #16
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    Quote Originally Posted by pike View Post
    Hello elmnas,
    Not knowing the content of the cells
    Option Explicit
    Sub Test()
        Dim myStrArray As Variant
        Dim Col As String
        Dim xRow As Long
        Dim Result As Double
        Dim iCount As Long
        
        myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
        For iCount = LBound(myStrArray) To UBound(myStrArray)
            If Not IsError(myStrArray(iCount)) Then
                For xRow = 2 To ActiveSheet.Cells(Rows.Count, myStrArray(iCount)).End(xlUp).Row
                    Result = Result + IIf(IsNumeric(Cells(xRow, myStrArray(iCount)).Value), Cells(xRow, myStrArray(iCount)).Value, 0)
                Next xRow
            End If
        Next iCount
        MsgBox Result
    End Sub

    I believe I have explained wrong cause now it loops each row and sum all cells for the column then next column

    I want the function to take every column and the cells same row and make a total then go to next row and to the same

  17. #17
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    no problem ..try
    Option Explicit
    Sub Test()
        Dim myStrArray As Variant
        Dim Col As String
        Dim xRow As Long
        Dim Result As Double
        Dim iCount As Long
        
        myStrArray = Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
        For iCount = LBound(myStrArray) To UBound(myStrArray)
            If Not IsError(myStrArray(iCount)) Then
                For xRow = 2 To ActiveSheet.Cells(Rows.Count, myStrArray(iCount)).End(xlUp).Row
                    Result = Result + IIf(IsNumeric(Cells(xRow, myStrArray(iCount)).Value), Cells(xRow, myStrArray(iCount)).Value, 0)
                Next xRow
            End If
        MsgBox Result
        Result = 0
        Next iCount
    End Sub

  18. #18
    VBAX Regular pike's Avatar
    Joined
    Dec 2007
    Location
    Alstonville, Australia
    Posts
    97
    Location
    or
    Option Explicit
    Sub Test()
        Dim myStrArray As Variant
        Dim Col As String
        Dim xRow As Long
        Dim Result As Double
        
        For Each myStrArray In Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")
            If Not IsError(myStrArray) Then
                For xRow = 2 To ActiveSheet.Cells(Rows.Count, myStrArray).End(xlUp).Row
                    Result = Result + IIf(IsNumeric(Cells(xRow, myStrArray).Value), Cells(xRow, myStrArray).Value, 0)
                Next xRow
            End If
        MsgBox Result
        Result = 0
        Next
        
    End Sub

  19. #19
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    What is the use of the empty element in array ???

    Array("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y")

  20. #20
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    114
    Location
    Here is a result I want,

    sum all cells for each row and following columns("N", "O", "P", "R", "S", "T", , "U", "V", "W", "X", "Y") in AB


    yqAdIhY.jpg

Tags for this Thread

Posting Permissions

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