Adding a column of numbers based on an odd or even cell

jUStPunkin

Board Regular
Joined
Mar 23, 2009
Messages
67
I have a cell, number of collection devices. Based on that number of collection devices, I'm going to have a number of sections between those devices.

If the number of devices is 5, for example, I'm going to have sections 1, 2, 3, 2, 1 (where 1 is the same as 1, 2 is the same as 2, 3 is unique)

If the number of devices is 6, for example, I'm going to have sections 1, 2, 3, 3, 2, 1 (where 1 is the same as 1, 2 is the same as 2 and 3 is the same as 3)

I then do a variety of calculations based on a number for each of those sections.

The trouble I'm having is I need to do a sum on those calculations. So I'll have a column of numbers, and I need to sum 1*2, 2*2 3*1; or 1*2, 2*2, 3*2.

I know that if the number of devices is odd; it will be 1*2, 2*2 3*1

If the number of devices is even, it will be 1*2, 2*2, 3*2.

And, to add further complexity; the number of devices could be anywhere between 2 and 20.

I'm trying to be as clear as possible, but I'm not sure if it's clear. In the screenshot, the highlighted number is the number of devices.

For the blue numbers, which are inputs, I want to be able to enter only half of them, and then add them based on whether the number of devices is odd or even, if it is even, you double all the numbers, if it's odd, you double all but the last number.

What I'm thinking I might have to do is something like (and I am not sure my syntax is correct), with just multiple cases. I'm just wondering if there is an easier, shorter way to do this?
VBA Code:
Public Sub NumberOfDevices()

Dim devices As Integer, result As Integer

devices = Range("b8").Value

Select Case devices

    Case Is = 3
        result = Sum(("A20"), ("A20"), ("A21"))
    Case Is = 4
        result = Sum(("A20"), ("A20"), ("A21"), ("A21"))
        
End Sub
 

Attachments

  • Screenshot 2022-03-11 090403.jpg
    Screenshot 2022-03-11 090403.jpg
    56 KB · Views: 14

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Do you mean something like this?

ABCD
1
2Length1112
3
4Result (VBA)6.905.30
5Result (Formula)6.905.30
6Check6.905.30
7
8Show ...0.20.10
90.30.15
100.50.25
110.70.40
121.10.75
131.31.00
14No need to show ...1.11.00
150.70.75
160.50.40
170.30.25
180.20.15
190.10
Sheet1
Cell Formulas
RangeFormula
B4,D4B4=mytotal(B8:B13,B2)
B5,D5B5=2*SUMPRODUCT(B8:INDEX(B:B,ROW(A8)+B2/2-1))+ISODD(B2)*INDEX(B:B,ROW(A8)+B2/2)
B6,D6B6=SUM(B8:B19)

VBA Code:
Function MyTotal(r As Range, N As Long) As Double

    MyTotal = 2 * Application.Sum(r) - (N Mod 2) * r((N + 1) / 2).Value

End Function
 
Upvote 0
Do you mean something like this?

ABCD
1
2Length1112
3
4Result (VBA)6.905.30
5Result (Formula)6.905.30
6Check6.905.30
7
8Show ...0.20.10
90.30.15
100.50.25
110.70.40
121.10.75
131.31.00
14No need to show ...1.11.00
150.70.75
160.50.40
170.30.25
180.20.15
190.10
Sheet1
Cell Formulas
RangeFormula
B4,D4B4=mytotal(B8:B13,B2)
B5,D5B5=2*SUMPRODUCT(B8:INDEX(B:B,ROW(A8)+B2/2-1))+ISODD(B2)*INDEX(B:B,ROW(A8)+B2/2)
B6,D6B6=SUM(B8:B19)

VBA Code:
Function MyTotal(r As Range, N As Long) As Double

    MyTotal = 2 * Application.Sum(r) - (N Mod 2) * r((N + 1) / 2).Value

End Function
That looks like what I need. I'll play around with this today and see if I can make it work. If so, I'll mark this as a solution; if not, I'll be back with more questions :)

Thank you!!
 
Upvote 0
I feel like I'm missing the easy part of this. I am able to make this work for odd cases with a little change

Excel Formula:
=2*SUMPRODUCT(B20:INDEX(B:B,ROW(A20)+B8/2))+ISODD(B8)*INDEX(B:B,ROW(A20)+B8/2+1)

But I can't make it work for even cases. It's this
Excel Formula:
 2*SUMPRODUCT(B20:INDEX(B:B,ROW(A20)+B8/2))
but I have to add the next cell as well, but not double it, just add the cell.

This should be the easy part, I just can't seem to figure how to get it into that array (and thank you for that, I am not really familiar with arrays, but I did learn a lot from this simple formula).
 
Upvote 0
Based on the data in B8 (number of downcomers), I need to be able to add the data in cells B8 through B25 (keeping in mind that this example only goes through B25, but they could go all the way through B40). If B8 is even; cells B8 through B25 are summed, and then multiplied x 2. If B8 is odd, cells B8 through B24 are added and doubled, and then B25 is added.

Is that clearer?

RedistPage.xlsm
ABCD
4ServiceService
5DateDate
6Designed by:Designed
7Comments:Comments
8Number of Downcomers10
9Liquid Rate (lb/hr)Density (lb/ft3)
10From Distributor1,006,56037.62926,749.581
11From Tray Above3,077,69935.19087,459.477
12Total114,209.059
13
14Collected From Above15.58%(Actual)
15Net From Trough1,486,06636.80640,375.768
16Net From Above2,598,19335.19073,833.291
17Total114,209.059
18
19Panel% of liquid feeding each panel (ideal)Total Liquid to each panelLiquid from tray above to each panel
2012.630%3,003.6982,533.784
2127.600%8,679.8888,863.817
2239.870%11,272.43412,849.919
23411.400%13,019.83315,474.511
24512.240%13,979.18916,831.697
25612.520%14,298.97417,279.562
267  
278  
289  
2910  
3011  
3112  
3213  
3314  
3415  
3516  
3617  
3718  
3819  
3920  
4021  
41112.52%64,254.01673,833.291
Redistribution Calculations
Cell Formulas
RangeFormula
D10D10=$B$10/$C$10
D11D11=$B$11/$C$11
D12,D17D12=SUM(D10:D11)
B15,D15B15=B10+$B$14*B11
C15:C16C15=B15/D15
B16,D16B16=B11*(1-$B$14)
C20:C40C20=IF(ISBLANK(B20),"",$D$17*B20)
D20:D40D20=IF(ISBLANK(B20),"",I20*$D$16)
B41B41=2*SUMPRODUCT($B$20:INDEX(B:B,ROW($A$20)+$B$8/2))+ISODD($B$8)*INDEX(B:B,ROW($A$20)+$B$8/2+1)
C41:D41C41=SUM(C20:C40)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
L33:P35,L29:O30,G42:H42,C1:D3,Q:IV,P1:P24,L1:O26,B55:E65536,A1:A46,A49:A65536,F1:F13,F15:F41,L41:P65536,B1:B14,G1:H18,G41,E1:E54,F44:H65536,H19:H41,C8:D14,I:K,B15:D54Expression=NOT(CELL("protect",A1))textYES
Cells with Data Validation
CellAllowCriteria
B11Any value
C11Any value
B14Any value
B20:B40Any value
 
Upvote 0
Thanks, yes that's clearer.

My original formula assumed that if number of downcomers = 10, say, you'd be taking twice the sum of 5 numbers. But that doesn't appear to be the case?

Will your numbers in B20:B40 always be an increasing sequence? If so, you could simplify to:

AB
8Number of Downcomers10
9
10
11
12
13
14
15
16
17
18
19Panel% of liquid feeding each panel (ideal)
2012.63%
2127.60%
2239.87%
23411.40%
24512.24%
25612.52%
267
278
289
2910
3011
3112
3213
3314
3415
3516
3617
3718
3819
3920
4021
41Required result?112.52%
Sheet2
Cell Formulas
RangeFormula
B41B41=2*SUM(B20:B40)-ISODD(B8)*MAX(B20:B40)
 
Upvote 0
Solution
Oh my gosh; that's so simple! I just have to check to see that all columns (there are more that will have the same operation performed on them; I just didn't/could't share the whole spreadsheet. But I do believe everything should be increasing.

Yea, I know it was convoluted before, but I had to figure out what exactly I could share here; there are definitely some confidential calcs in our spreadsheets. But I think you nailed it

Thank you!!
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top