How do I sum data based on a PART of the headers name?

后端 未结 2 1443
粉色の甜心
粉色の甜心 2020-12-22 01:10

Say I have columns

/670 - White | /650 - black | /680 - Red | /800 - Whitest

These have data in their rows. Basically, I want to SUM their values together

2条回答
  •  萌比男神i
    2020-12-22 01:31

    I start this solution with the names in an array, you can read the header names into an array with not too much difficulty.

    Sub test()
        Dim myArray(1 To 4) As String
        myArray(1) = "/670 - White"
        myArray(2) = "/650 - black"
        myArray(3) = "/680 - Red"
        myArray(4) = "/800 - Whitest"
    
        For Each ArrayValue In myArray
        'Find position of last character
            endposition = InStr(1, ArrayValue, " - ", vbTextCompare)
        'Grab the number section from the string, based on starting and ending positions
            stringvalue = Mid(ArrayValue, 2, endposition - 2)
        'Convert to number
            NumberValue = CLng(stringvalue)
        'Add to total
            Total = Total + NumberValue
        Next ArrayValue
        'Print total
        Debug.Print Total
    End Sub
    

    This will print the answer to the debug window.

提交回复
热议问题