ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link เกี่ยวกับ AI
    • รวม Link Coding
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

เจาะลึกสูตร IF ใน Excel

excel if

Categories 📂

Excel ทั่วไป

Tags 🏷️

IF

สูตร IF คืออะไร?

สูตร IF (หรือจะเรียกให้ถูกคือฟังก์ชัน IF) คือ ฟังก์ชั่น (Function) ซึ่งทำหน้าที่เลือกใช้สูตรคำนวณได้ 2 แบบ ขึ้นอยู่กับเงื่อนไขที่เราระบุลงไปว่าจริงหรือเท็จ?

ถ้าเงื่อนไขเป็นจริง (TRUE) จะทำการคำนวณด้วยสูตรแบบหนึ่ง ถ้าเงื่อนไขเป็นเท็จ (FALSE) จะคำนวณด้วยสูตรอีกแบบหนึ่ง

** ผมใช้คำว่า “สูตร” เพราะใส่สูตรยาวๆ ซับซ้อนแค่ไหนก็ได้

ผมแนะนำให้มองว่า IF 1 ตัว สามารถแตกกิ่งก้านสาขาการตัดสินใจ (Decision Tree) ออกไปได้ 2 กิ่ง
คือ ผลลัพธ์จากกรณีเงื่อนไขเป็น TRUE และ ผลลัพธ์จากกรณีเงื่อนไขเป็น FALSE

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

ปล. ผมมีเขียนบทความใหม่เกี่ยวกับ IF รวมถึงตัวที่ใกล้เคียงกับมัน เช่น IFS และ SWITCH เอาไว้ด้วย สามารถไปอ่านได้ที่นี่

วิธีเขียนสูตร

=IF(logical_test,[value_if_true],[value_if_false])
=IF(เงื่อนไขที่เราโยนเข้าไปให้ทดสอบ, [ถ้าจริงจะทำอันนี้], [ถ้าเท็จจะทำอันนี้])

เช่น หากเทียบกับภาษาพูด จะได้ว่า

  • ถ้า ฉันสอบตก ฉันจะเลี้ยงข้าว ไม่งั้น เธอเป็นคนเลี้ยงข้าว
    • ฉันสอบตก = เงื่อนไข
    • ฉันเลี้ยงข้าว = การกระทำหากเงื่อนไขเป็นจริง
    • เธอเป็นคนเลี้ยงข้าว = การกระทำหากเงื่อนไขเป็นเท็จ

ซึ่งเงื่อนไขที่เราโยนเข้าไปให้ทดสอบ นั้นจะต้องมี  ตัวเปรียบเทียบ (COMPARISON OPERATOR) อยู่ด้วย เพื่อให้ค่าออกมาเป็นจริง (TRUE)  หรือเท็จ (FALSE) เช่น = เท่ากับ, < น้อยกว่า, > มากกว่า, <> ไม่เท่ากับ, >= มากกว่าหรือเท่ากับ, <= น้อยกว่าหรือเท่ากับ

ตัวอย่าง 1

สูตร IF

หากช่อง B4 เราเขียนว่า

=IF(B2>B3,"ของไม่พอ","ของพอ")

สิ่งที่มันจะทำคือดูว่าค่าใน B2 มากกว่า B3 หรือไม่?
ซึ่งในที่นี้มากกว่า แปลว่า Logical Test (เงื่อนไข) ได้ผลลัพธ์เป็น TRUE
ซึ่งเปรียบได้กับ =IF(TRUE,”ของไม่พอ”,”ของพอ”)

และเมื่อ Logical Test เป็น TRUE ก็จะแสดงผลลัพธ์จาก value_if_true ซึ่งก็คือ “ของไม่พอ” นั่นเองครับ

หากมีหลายเงื่อนไขจะทำอย่างไร?

โดยหากมีหลายเงื่อนไข เราสามารถเอาแต่ละเงื่อนไขมาเชื่อมกับฟังก์ชั่นทางตรรกศาสตร์ ได้อีก เช่น

  • AND(เงื่อนไข1,เงื่อนไข2,เงื่อนไข3,…) => และ : เงื่อนไขต้องเป็นจริงทุกอัน ผลลัพธ์ถึงจะออกมาเป็นจริง
  • OR(เงื่อนไข1,เงื่อนไข2,เงื่อนไข3,…) => หรือ : หากเงื่อนไขอันใดอันหนึ่งจริงถือว่าผลลัพธ์จริง

ตัวอย่าง 2

สูตร IF
สูตร IF

การเขียน IF ซ้อน IF

เราสามารถเขียน IF ซ้อนกันไปเรื่อยๆ ได้ (จริงๆแล้วจะเอาฟังก์ชั่นอื่นมาซ้อนด้วยก็ยังได้)
โดย IF แต่ละตัวก็จะมีการเช็คเงื่อนไขของตัวเอง และทำค่าจริง/เท็จ ของตัวเองแล้วแต่ผลลัพธ์ที่ประมวลได้ เช่น

=IF(เงื่อนไข 1, IF( เงื่อนไข2,จริง2,เท็จ2),IF(เงื่อนไข3,จริง3,เท็จ3))

เทคนิคการเขียน IF ซ้อนกันหลายตัว

  • จำนวนวงเล็บเปิดต้องเท่ากับวงเล็บปิด (ในที่นี้มีอย่างละ 4 อันเท่ากัน)
  • ในแต่ละกิ่งที่ จริง /เท็จ เราสามารถเลือกที่จะใส่ IF ซ้อนลงไปอีกหรือไม่ก็ได้ ลองดูได้จากตัวอย่างที่จะเห็นต่อไป
Nested IF Excel

ตัวอย่าง 3

หากเราต้องการจะจัดเกรดจากคะแนนดิบของนักเรียน โดยมีเกณฑ์ดังนี้

  • คะแนน <50 : F
  • 50 <= คะแนน<60 : D
  • 60 <= คะแนน<70  : C
  • 70 <= คะแนน<80  : B
  • คะแนน >= 80 : A

สมมติคะแนนดิบอยู่ในช่อง A2 และเราจะใส่เกรดในช่อง B2 / ในช่อง B2 เราต้องเขียนดังนี้

=IF(A2<50,"F",IF(A2<60,"D",IF(A2<70,"C",IF(A2<80,"B","A"))))

อธิบายแนวคิด

ให้ใส่เงื่อนไขทีละ Step อันแรกเช็คว่าน้อยกว่า 50 หรือไม่? ถ้าน้อยกว่าให้แสดงเกรด F ถ้าไม่น้อยกว่า ต้องเช็คต่อว่า < 60 หรือไม่?… ทำแบบนี้ไปเรื่อยๆ

Nested IF function Excel

สรุปแบบ Slide

Excel 9 หน้า : IF ถ้ารู้งี้ ก็เก่งไปนานแล้ว! from Sira Ekabut

หวังว่าเพื่อนๆ อ่านแล้วจะเข้าใจมากขึ้นนะครับใครอ่านแล้วสงสัยตรงไหน หรือมีข้อแนะนำอะไร อย่าลืม Comment ในนี้ หรือมาพูดคุยกันได้ใน Facebook นะครับ

แชร์ความรู้ให้เพื่อนๆ ของคุณ
89    
89    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • วิธีผสม Model สูตรผสมแบบโดนใจใน Stable Diffusion [Part7]
  • สอนทำรูปตัวเองคู่กับสาว ใน Stable Diffusion [Part6]
  • สอน Train Model ตัวเองใน Stable Diffusion [Part5]
  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • บันได 10 ขั้น เริ่มเรียนรู้ M Code ขุมพลังของ Power Query
  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง

Categories

Tags

ai collection concepts copy database Data Model data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro len link logic lookup match matrix mcode m code merge mid overview paste pivot power query row sort speed split stable diffusion substitute table text time tips trim vba vlookup

Archives

  • April 2023 (8)
  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT