奇数项求和公式_Excel公式求和奇数或偶数周
奇數項求和公式
Here's an Excel formula challenge for you, based on an email question that someone sent to me. They wanted help with an Excel formula to sum for odd and even weeks. See my solution, and let me know how you'd solve the problem.
根據某人發送給我的電子郵件問題,這是您面臨的Excel公式挑戰。 他們希望獲得有關Excel公式的幫助,以求奇數和偶數周的總和。 查看我的解決方案,讓我知道您將如何解決問題。
為什么總和是奇數,甚至是幾周? (Why Sum Odd or Even Weeks?)
Have you ever needed to add numbers, based on odd and even numbers in another column?
您是否曾經需要根據另一列中的奇數和偶數來添加數字?
In this example, the person worked in a factory, where there are 2 work crews – Crew A and Crew B. They need to compare the production quantities for the 2 work crews.
在此示例中,此人在一家工廠工作,那里有2名工作人員- 機組A和機組B。 他們需要比較兩個工作人員的生產數量。
Here's a simplified version of the production data, which we'll use for this challenge. As you can see, the crew name is not in the data.
這是生產數據的簡化版本,我們將用它來應對這一挑戰。 如您所見,數據中沒有機組人員名稱。
This is the morning data from the first 4 weeks of the year.
這是一年前4周的早晨數據。
識別機組人員 (Identify the Crews)
Even though the crew name is not listed, we can use the week numbers to identify which crew was working.
即使沒有列出機組人員的姓名,我們也可以使用周號來標識正在工作的機組人員。
Crew A works mornings on odd-numbered weeks
機組A在奇數周的早晨工作
Crew B works mornings on even-numbered weeks
機組B在偶數周的早晨工作
The production data doesn't show the crew name, but we can total the odd or even week data, to get each crew's total quantity.
生產數據沒有顯示機組人員的姓名,但是我們可以合計奇數或偶數周數據,以獲得每個機組人員的總數。
方程式挑戰賽 (Formula Challenge)
Your challenge is to create a formula to calculate the total quantities for odd and even weeks
您面臨的挑戰是創建一個公式來計算奇數周和偶數周的總量
You can type the data in a blank workbook, or download my sample file with the challenge data and all the solutions that are shown below.
您可以在空白工作簿中鍵入數據,或下載包含挑戰數據和下面顯示的所有解決方案的示例文件 。
您有什么解決方案? (What's Your Solution?)
You can see my solution in the next section, and below that, you'll see solutions from my weekly Excel newsletter readers.
您可以在下一節中看到我的解決方案,在其下,您將看到每周的Excel新聞快訊讀者的解決方案。
What pros and cons can you see in the other people's solutions?
您在其他人的解決方案中可以看到哪些利弊?
If you found a different solution than the ones shown here, let me know in the comment section.
如果您找到的解決方案與此處顯示的解決方案不同,請在評論部分告知我。
我的解決方案 (My Solution)
Here's my formula to sum for odd or even weeks. I used the SUMPRODUCT function, combined with ISODD and ISEVEN.
這是我求和奇數或偶數周的公式。 我將SUMPRODUCT函數與ISODD和ISEVEN結合使用。
I put this formula in cell E2, to total the odd weeks:
我將此公式放在單元格E2中,以得出奇數周總數:
=SUMPRODUCT((ISODD(--($B$2:$B$11))) *($A$2:$A$11))
= SUMPRODUCT((ISODD(-($ B $ 2:$ B $ 11)))*($ A $ 2:$ A $ 11))
And here's the formula in cell E3, to total the even weeks:
這是單元格E3中的公式,總共為偶數周:
=SUMPRODUCT((ISEVEN(--($B$2:$B$11))) *($A$2:$A$11))
= SUMPRODUCT((ISEVEN(-($ B $ 2:$ B $ 11)))*($ A $ 2:$ A $ 11))
這個怎么運作 (How It Works)
Here's how my formula works:
這是我的公式的工作原理:
- The ISODD and ISEVEN functions return TRUE or FALSE ISODD和ISEVEN函數返回TRUE或FALSE
- The two minus signs (double unary) convert those T/F values to numbers (-1 or 0) 兩個減號(雙一進制)將這些T / F值轉換為數字(-1或0)
- Those results are multiplied by the Qty amounts 這些結果乘以數量
- The SUMPRODUCT function returns the total of all those multiplications. SUMPRODUCT函數返回所有這些乘法的總和。
There are more Sumproduct examples on my Contextures website.
我的Contextures網站上有更多Sumproduct示例 。
其他解決方案 (Other Solutions)
Next, here are some of the formulas that newsletter readers sent to me. You can get the data, and see all of the solutions in the Formula Challenge sample file.
接下來,這是時事通訊讀者發送給我的一些公式。 您可以獲取數據,并在Formula Challenge示例文件中查看所有解決方案。
MOD功能 (MOD Function)
Many of the solutions used the MOD function, to check if the week numbers were odd or even.
許多解決方案使用MOD功能來檢查星期數是奇數還是偶數。
- The MOD function returns the remainder when you divide the first number (week number) by the second number (2) 當您將第一個數字(周數)除以第二個數字(2)時,MOD函數將返回余數
- If you divide an even number by 2, the remainder will be zero 如果將偶數除以2,則余數將為零
- If you divide an odd number by 2 the remainder will be 1 如果將奇數除以2,則余數將為1
For example, to find the total for odd weeks:
例如,要查找奇數周的總數:
=SUMPRODUCT((MOD($B$2:$B$11,2)=1)*$A$2:$A$11)
= SUMPRODUCT(( MOD($ B $ 2:$ B $ 11,2)= 1 )* $ A $ 2:$ A $ 11)
SUM或SUMPRODUCT (SUM or SUMPRODUCT)
All the solutions used SUM or SUMPRODUCT to calculate the grand total.
所有解決方案都使用SUM或SUMPRODUCT來計算總計。
For example, to find the total for even weeks:
例如,要查找偶數周的總數:
=SUM(MOD($B$2:$B$11-1,2)*$A$2:$A$11)
= SUM(MOD($ B $ 2:$ B $ 11-1,2)* $ A $ 2:$ A $ 11)
NOTE: Array Formulas
注意:數組公式
In older versions of Excel, you'll need to array-enter the SUM? formulas, with Ctrl+Shift+Enter.
在舊版Excel中,您需要使用Ctrl + Shift + Enter 數組輸入SUM公式 。
- If your version of Excel has Dynamic Array formulas, you won't need to do that -- just press Enter. 如果您的Excel版本具有動態數組公式,則無需這樣做-只需按Enter。
過濾功能 (FILTER Function)
Two of the solutions use the new FILTER function. It filters a set of numbers, based on a rule.
其中兩個解決方案使用新的FILTER函數。 它根據規則過濾一組數字。
=SUM(FILTER($A$2:$A$11, MOD($B$2:$B$11,2)))
= SUM( FILTER ($ A $ 2:$ A $ 11,MOD($ B $ 2:$ B $ 11,2)))
In these solutions, the quantities were returned, based on using the MOD function in the week number column.
在這些解決方案中,基于周數列中的MOD函數返回了數量。
You can use these solutions if your version of Excel has Dynamic Array formulas.
如果您的Excel版本具有動態數組公式,則可以使用這些解決方案。
硬編碼值 (Hard-Coded Values)
In some solutions, including my original solution, there were hard-coded values. For example, the Odd week formulas used a 1, and the Even week formulas used a zero.
在某些解決方案中,包括我的原始解決方案,都有硬編碼的值。 例如,奇數周公式使用1,而偶數周公式使用0。
=SUMPRODUCT((MOD($B$2:$B$11,2)=1) *$A$2:$A$11)
= SUMPRODUCT((MOD($ B $ 2:$ B $ 11,2) = 1 )* $ A $ 2:$ A $ 11)
=SUMPRODUCT((MOD($B$2:$B$11,2)=0) *$A$2:$A$11)
= SUMPRODUCT((MOD($ B $ 2:$ B $ 11,2) = 0 )* $ A $ 2:$ A $ 11)
You could put those values in adjacent cells, and refer to those cells in the formula.
您可以將這些值放在相鄰的單元格中,然后在公式中引用這些單元格。
=SUMPRODUCT((MOD($B$2:$B$11,2)=D2)*$A$2:$A$11)
= SUMPRODUCT((MOD($ B $ 2:$ B $ 11,2) = D2 )* $ A $ 2:$ A $ 11)
Then, drag down, to use the same formula in both calculations. I like to use consistent formulas wherever possible!
然后,向下拖動以在兩個計算中使用相同的公式。 我喜歡盡可能使用一致的公式!
Here's how I should have written my original solutions, to make it the same in both cells.
這是我應該編寫原始解決方案的方式,以使其在兩個單元中都相同。
- In cell D2, type TRUE, and in D3, type FALSE 在單元格D2中,鍵入TRUE,在D3中,鍵入FALSE
- In cell E2, enter this formula, then copy it down to cell E3 在單元格E2中,輸入此公式,然后將其復制到單元格E3中
=SUMPRODUCT((ISODD(--($B$2:$B$11))=D2) * ($A$2:$A$11))
= SUMPRODUCT((ISODD(-($ B $ 2:$ B $ 11))= D2)*($ A $ 2:$ A $ 11))
硬編碼陣列 (Hard-Coded Array)
One solution used hard-coded arrays of odd and even numbers.
一種解決方案是使用奇數和偶數的硬編碼數組。
=SUM(SUMIFS($A$2:$A$11,$B$2:$B$11,{1,3}))
= SUM(SUMIFS($ A $ 2:$ A $ 11,$ B $ 2:$ B $ 11 , {1,3} ))
=SUM(SUMIFS($A$2:$A$11,$B$2:$B$11,{2,4}))
= SUM(SUMIFS($ A $ 2:$ A $ 11,$ B $ 2:$ B $ 11, {2,4} ))
That worked alright in this small sample, with only 4 work weeks, but you'd need a flexible solution for larger samples.
在這個只有4個工作周的小樣本中,這種方法可以正常工作,但是您需要一個靈活的解決方案來處理較大的樣本。
助手列 (Helper Columns)
A few people used one or two helper columns in their solutions.? You can see those in the sample file, and I've colour coded the columns, to match the solutions in which they're used.
少數人在解決方案中使用了一兩個輔助列。 您可以在示例文件中看到這些內容,并且已經對這些列進行了顏色編碼,以匹配使用它們的解決方案。
Helper cells or columns are useful in some situations, allowing you to break a complex formula into smaller sections that are easier to understand or troubleshoot.
輔助單元格或列在某些情況下很有用,可讓您將復雜的公式分解為較小的部分,以便于理解或排除故障。
But in these solutions, you'd need formulas in every data row, instead of a just 2 formulas at the top of the worksheet.
但是在這些解決方案中,您需要在每個數據行中使用公式,而不是在工作表頂部僅使用2個公式。
That's not too bad in a small file like this one, but could really slow things down if you're working with thousands of rows of data.
在像這樣的小文件中,這還不錯,但是如果您要處理成千上萬的數據行,則確實可能使速度變慢。
更多總和示例 (More Sum Examples)
There are more How to Sum examples on my Contextures website.
我的Contextures網站上有更多“如何求和”示例 。
And remember, you can get challenge data, and see all of the solutions in the Formula Challenge sample file.
請記住,您可以獲得挑戰數據,并在Formula Challenge示例文件中查看所有解決方案。
翻譯自: https://contexturesblog.com/archives/2020/04/30/excel-formula-to-sum-for-odd-or-even-weeks/
奇數項求和公式
總結
以上是生活随笔為你收集整理的奇数项求和公式_Excel公式求和奇数或偶数周的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广和通5G模组FG650-CN、FM65
- 下一篇: 中控科技 ZK Software的售后服