Consulting

Results 1 to 7 of 7

Thread: How to sum cells based on ?? (array? constants?)

  1. #1
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location

    How to sum cells based on ?? (array? constants?)

    A B C
    1 CEO 0.0 0.0
    2 G 60.0 60.0
    3 COO 0.0 0.0
    4 O 5.0 5.0
    5 E 13107.0 13107.0
    6 PROD 45.0 45.0
    7 OE 3.0 3.0
    8 D 1182.2 1182.2
    9 DS 1372.3 1372.3
    10 DC 513.0 513.0
    11 P 379.0 379.0

    In this example, I want red cells B3,C3 to sum the cells in row yellow BUT it should comes from a constant or an array, so that user has just one line of code to change when it evolves (he can go into code, but simple one)

    I've thought about having a string constant, that would become my array, but i'm getting totally lost, it's too hard for me... Do you have a better idea?
    Public Const ACOO As String = "O,E,PROD,OE,OT,P,Q,I,J,PROD,S,BD,BM"
    
    For i = 1 To 4
        T_COOO = Array(ACOO)
        My_ID = T_COOO(i)
    
        For MyRow = 2 To 56
            If Cells(My_Row, CSIG) = My_ID Then
            My_Total = My_Total + Cells(My_Row, 2)
            End If
        Next MyRow
    
    Cells(RSN4, 2) = My_Total
    Next i

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    T_COOO = Split(ACOO, ",")
    For i = 0 To 3
      For MyRow = 2 To 56
        If Cells(My_Row, CSIG) = T_COOO(i) Then My_Total = My_Total + Cells(My_Row, 2)
      Next MyRow
      Cells(RSN4, 2) = My_Total
    Next i
    ?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location
    I'll try that on Monday. I have a doubt: As the lengh of string ACOO is unknown, what will happen if "i" is too big? can I solve it by a simple "On error resume next"?

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by ValerieT View Post
    I'll try that on Monday. I have a doubt: As the lengh of string ACOO is unknown, what will happen if "i" is too big? can I solve it by a simple "On error resume next"?
    For i = 0 To UBound(T_COOO)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    to sum the data in column B, according to the value conditions in column A:

    Sub M_snb()
    y = [sum(if(iserror(match(A2:A56,{"E","I","J","O","P","Q","S","BD","BM","OE","OT ","PROD"},0)),0,B2:B56))]
    End Sub

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    to sum the data in column B, according to the value conditions in column A:

    Sub M_snb()
    y = [sum(if(iserror(match(A2:A56,{"E","I","J","O","P","Q","S","BD","BM","OE","OT","PROD"},0)),0,B2:B56))]
    End Sub

  7. #7
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location
    I finally has time to test it. After some errors, it works fine. I thank you very much

Posting Permissions

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