วิธีใช้ฟังก์ชันลอจิกใน Excel: IF, AND, OR, XOR, NOT

ฟังก์ชันลอจิกเป็นฟังก์ชันยอดนิยมและมีประโยชน์ใน Excel พวกเขาสามารถทดสอบค่าในเซลล์อื่นและดำเนินการตามผลของการทดสอบ สิ่งนี้ช่วยให้เราทำงานอัตโนมัติในสเปรดชีตของเรา

วิธีใช้ฟังก์ชัน IF

ฟังก์ชัน IF เป็นฟังก์ชันลอจิคัลหลักใน Excel ดังนั้นจึงเป็นฟังก์ชันที่ต้องทำความเข้าใจก่อน จะปรากฏหลายครั้งตลอดบทความนี้

มาดูโครงสร้างของฟังก์ชัน IF จากนั้นดูตัวอย่างการใช้งาน

ฟังก์ชัน IF ยอมรับข้อมูล 3 บิต:

= IF (logical_test, [value_if_true], [value_if_false])
  • logical_test:นี่คือเงื่อนไขสำหรับฟังก์ชันที่จะตรวจสอบ
  • value_if_true:การดำเนินการหากตรงตามเงื่อนไขหรือเป็นจริง
  • value_if_false:การดำเนินการหากไม่ตรงตามเงื่อนไขหรือเป็นเท็จ

ตัวดำเนินการเปรียบเทียบที่จะใช้กับฟังก์ชันลอจิคัล

เมื่อทำการทดสอบตรรกะกับค่าเซลล์คุณต้องคุ้นเคยกับตัวดำเนินการเปรียบเทียบ คุณสามารถดูรายละเอียดของสิ่งเหล่านี้ได้ในตารางด้านล่าง

ตอนนี้เรามาดูตัวอย่างการใช้งานจริง

ตัวอย่างฟังก์ชัน IF 1: ค่าข้อความ

ในตัวอย่างนี้เราต้องการทดสอบว่าเซลล์เท่ากับวลีเฉพาะหรือไม่ ฟังก์ชัน IF ไม่คำนึงถึงตัวพิมพ์เล็กและใหญ่ดังนั้นจึงไม่คำนึงถึงอักษรตัวพิมพ์ใหญ่และตัวพิมพ์เล็ก

สูตรต่อไปนี้ใช้ในคอลัมน์ C เพื่อแสดง“ ไม่ใช่” หากคอลัมน์ B มีข้อความ“ เสร็จสมบูรณ์” และ“ ใช่” หากมีสิ่งอื่นใด

= IF (B2 = "เสร็จสมบูรณ์", "ไม่", "ใช่")

แม้ว่าฟังก์ชัน IF จะไม่คำนึงถึงขนาดตัวพิมพ์ แต่ข้อความจะต้องตรงกันทุกประการ

ตัวอย่างฟังก์ชัน IF 2: ค่าตัวเลข

ฟังก์ชัน IF เหมาะอย่างยิ่งสำหรับการเปรียบเทียบค่าตัวเลข

ในสูตรด้านล่างเราทดสอบว่าเซลล์ B2 มีตัวเลขมากกว่าหรือเท่ากับ 75 หรือไม่ถ้าเป็นเช่นนั้นเราจะแสดงคำว่า "ผ่าน" และถ้าไม่ใช่คำว่า "Fail"

= IF (B2> = 75, "ผ่าน", "Fail")

ฟังก์ชัน IF เป็นมากกว่าการแสดงข้อความที่แตกต่างกันในผลลัพธ์ของการทดสอบ เรายังสามารถใช้เพื่อเรียกใช้การคำนวณต่างๆ

ในตัวอย่างนี้เราต้องการให้ส่วนลด 10% หากลูกค้าใช้จ่ายเงินจำนวนหนึ่ง เราจะใช้ 3,000 ปอนด์เป็นตัวอย่าง

= IF (B2> = 3000, B2 * 90%, บี 2)

ส่วน B2 * 90% ของสูตรเป็นวิธีที่คุณสามารถลบ 10% จากค่าในเซลล์ B2 มีหลายวิธีในการทำเช่นนี้

สิ่งสำคัญคือคุณสามารถใช้สูตรใดก็ได้ในส่วนvalue_if_trueหรือ value_if_falseและการเรียกใช้สูตรที่แตกต่างกันขึ้นอยู่กับค่าของเซลล์อื่นเป็นทักษะที่ทรงพลังมากที่จะมี

ฟังก์ชัน IF ตัวอย่างที่ 3: ค่าวันที่

ในตัวอย่างที่สามนี้เราใช้ฟังก์ชัน IF เพื่อติดตามรายการวันที่ครบกำหนด เราต้องการแสดงคำว่า "เกินกำหนด" หากวันที่ในคอลัมน์ B เป็นวันที่ในอดีต แต่ถ้าวันที่อยู่ในอนาคตให้คำนวณจำนวนวันจนถึงวันที่ครบกำหนด

สูตรด้านล่างใช้ในคอลัมน์ C เราตรวจสอบว่าวันที่ครบกำหนดในเซลล์ B2 น้อยกว่าวันที่ของวันนี้หรือไม่ (ฟังก์ชัน TODAY จะส่งคืนวันที่ของวันนี้จากนาฬิกาของคอมพิวเตอร์)

Original text


= IF (B2

สูตร IF ซ้อนกันคืออะไร?

คุณอาจเคยได้ยินคำว่า IF ที่ซ้อนกันมาก่อน ซึ่งหมายความว่าเราสามารถเขียนฟังก์ชัน IF ภายในฟังก์ชัน IF อื่นได้ เราอาจต้องการทำสิ่งนี้หากเรามีการดำเนินการมากกว่าสองรายการ

ฟังก์ชัน IF หนึ่งฟังก์ชันสามารถดำเนินการได้สองอย่าง ( value_if_trueและvalue_if_false) แต่ถ้าเราฝัง (หรือซ้อน) ฟังก์ชัน IF อื่นในvalue_if_falseส่วนนี้เราสามารถดำเนินการอื่นได้

ใช้ตัวอย่างนี้ที่เราต้องการแสดงคำว่า“ ยอดเยี่ยม” หากค่าในเซลล์ B2 มากกว่าหรือเท่ากับ 90 ให้แสดง“ ดี” หากค่ามากกว่าหรือเท่ากับ 75 และแสดง“ แย่” หากมีสิ่งอื่นใด .

= IF (B2> = 90, "ดีเยี่ยม", IF (B2> = 75, "ดี", "แย่"))

ตอนนี้เราได้ขยายสูตรของเราไปให้ไกลกว่าสิ่งที่ฟังก์ชัน IF ทำได้เพียงตัวเดียว และคุณสามารถซ้อนฟังก์ชัน IF เพิ่มเติมได้หากจำเป็น

สังเกตวงเล็บปิดสองอันที่ส่วนท้ายของสูตรหนึ่งอันสำหรับแต่ละฟังก์ชัน IF

มีสูตรอื่นที่สามารถทำความสะอาดได้ดีกว่าวิธี IF แบบซ้อนกันนี้ ทางเลือกหนึ่งที่มีประโยชน์มากคือฟังก์ชัน SWITCH ใน Excel

ฟังก์ชัน AND และ OR

ฟังก์ชัน AND และ OR ใช้เมื่อคุณต้องการทำการเปรียบเทียบมากกว่าหนึ่งรายการในสูตรของคุณ ฟังก์ชัน IF เพียงอย่างเดียวสามารถจัดการกับเงื่อนไขเดียวหรือการเปรียบเทียบเท่านั้น

ลองดูตัวอย่างที่เราลดมูลค่า 10% ขึ้นอยู่กับจำนวนเงินที่ลูกค้าใช้จ่ายและจำนวนปีที่พวกเขาเป็นลูกค้า

ด้วยตัวเองฟังก์ชัน AND และ OR จะส่งคืนค่าเป็น TRUE หรือ FALSE

The AND function returns TRUE only if every condition is met, and otherwise returns FALSE. The OR function returns TRUE if one or all of the conditions are met, and returns FALSE only if no conditions are met.

These functions can test up to 255 conditions, so are certainly not limited to just two conditions like is demonstrated here.

Below is the structure of the AND and OR functions. They are written the same. Just substitute the name AND for OR. It is just their logic which is different.

=AND(logical1, [logical2] ...)

Let’s see an example of both of them evaluating two conditions.

AND Function example

The AND function is used below to test if the customer spends at least £3,000 and has been a customer for at least three years.

=AND(B2>=3000,C2>=3)

You can see that it returns FALSE for Matt and Terry because although they both meet one of the criteria, they need to meet both with the AND function.

OR Function Example

The OR function is used below to test if the customer spends at least £3,000 or has been a customer for at least three years.

=OR(B2>=3000,C2>=3)

In this example, the formula returns TRUE for Matt and Terry. Only Julie and Gillian fail both conditions and return the value of FALSE.

Using AND and OR with the IF Function

Because the AND and OR functions return the value of TRUE or FALSE when used alone, it’s rare to use them by themselves.

Instead, you’ll typically use them with the IF function, or within an Excel feature such as Conditional Formatting or Data Validation to perform some retrospective action if the formula evaluates to TRUE.

In the formula below, the AND function is nested inside the IF function’s logical test. If the AND function returns TRUE then 10% is discounted from the amount in column B; otherwise, no discount is given and the value in column B is repeated in column D.

=IF(AND(B2>=3000,C2>=3),B2*90%,B2)

The XOR Function

In addition to the OR function, there is also an exclusive OR function. This is called the XOR function. The XOR function was introduced with the Excel 2013 version.

This function can take some effort to understand, so a practical example is shown.

The structure of the XOR function is the same as the OR function.

=XOR(logical1, [logical2] ...)

When evaluating just two conditions the XOR function returns:

  • TRUE if either condition evaluates to TRUE.
  • FALSE if both conditions are TRUE, or neither condition is TRUE.

This differs from the OR function because that would return TRUE if both conditions were TRUE.

This function gets a little more confusing when more conditions are added. Then the XOR function returns:

  • TRUE if an odd number of conditions return TRUE.
  • FALSE if an even number of conditions result in TRUE, or if all conditions are FALSE.

Let’s look at a simple example of the XOR function.

In this example, sales are split over two halves of the year. If a salesperson sells £3,000 or more in both halves then they are assigned Gold standard. This is achieved with an AND function with IF like earlier in the article.

But if they sell £3,000 or more in either half then we want to assign them Silver status. If they don’t sell £3,000 or more in both then nothing.

The XOR function is perfect for this logic. The formula below is entered into column E and shows the XOR function with IF to display  “Yes” or “No” only if either condition is met.

=IF(XOR(B2>=3000,C2>=3000),"Yes","No")

The NOT Function

The final logical function to discuss in this article is the NOT function, and we have left the simplest for last. Although sometimes it can be hard to see the ‘real world’ uses of the function at first.

The NOT function reverses the value of its argument. So if the logical value is TRUE, then it returns FALSE. And if the logical value is FALSE, it will return TRUE.

This will be easier to explain with some examples.

The structure of the NOT function is;

=NOT(logical)

NOT Function Example 1

In this example, imagine we have a head office in London and then many other regional sites. We want to display the word “Yes” if the site is anything except London, and “No” if it is London.

The NOT function has been nested in the logical test of the IF function below to reverse the TRUE result.

=IF(NOT(B2="London"),"Yes","No")

This can also be achieved by using the NOT logical operator of . Below is an example.

=IF(B2"London","Yes","No")

NOT Function Example 2

The NOT function is useful when working with information functions in Excel. These are a group of functions in Excel that check something, and return TRUE if the check is a success, and FALSE if it is not.

For example, the ISTEXT function will check if a cell contains text and return TRUE if it does and FALSE if it does not. The NOT function is helpful because it can reverse the result of these functions.

In the example below, we want to pay a salesperson 5% of the amount they upsell. But if they did not upsell anything, the word “None” is in the cell and this will produce an error in the formula.

The ISTEXT function is used to check for the presence of text. This returns TRUE if there is text, so the NOT function reverses this to FALSE. And the IF performs its calculation.

=IF(NOT(ISTEXT(B2)),B2*5%,0)

Mastering logical functions will give you a big advantage as an Excel user. To be able to test and compare values in cells and perform different actions based on those results is very useful.

This article has covered the best logical functions used today. Recent versions of Excel have seen the introduction of more functions added to this library, such as the XOR function mentioned in this article. Keeping up to date with these new additions will keep you ahead of the crowd.