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

Excel VBA พื้นฐาน ตอนที่ 1 : เขียน Code ครั้งแรก

Categories 📂

Excel VBA, Highlights : บทความแนะนำ

Tags 🏷️

มีแฟนเพจหลายท่านอยากให้ผมสอนเรื่องเกี่ยวกับ VBA ให้ แม้ผมจะเคยเขียนเกี่ยวกับเรื่อง Concept สำคัญของ VBA ในแง่ของหลักการไปแล้ว แต่ผมยังไม่ได้สอนเขียน Code แบบจริงๆ จังๆ ซักที แต่ตอนนี้คงถึงเวลาแล้วล่ะ ^^

อ่อ! ในบทความนี้จะไม่ได้สอนเรื่อง record macro นะครับ อันนั้นลองไปหัดกันเองได้นะ หรือดูใน youtube ก็น่าจะมีเยอะเลย ในบทความนี้จะสอนหลักการการเขียนโปรแกรมให้ เพื่อให้เพื่อนๆ สามารถอ่าน code และแก้ code เองได้หลังจาก record macro แล้วนะครับ

สารบัญ

  • การเปิด Developer Ribbon
  • การ Trigger คำสั่งด้วยปุ่ม
  • การ Assign ค่า และเรื่องของตัวแปร
  • การอ้างอิง Cell/Range โดยใช้ Range
  • การใช้ Message Box
  • การใช้เครื่องหมาย & เชื่อมข้อความ
  • การใช้ IF กำหนดเงื่อนไข
  • การ Save ไฟล์
  • สรุป VBA พื้นฐาน ตอนที่ 1

การเปิด Developer Ribbon

เริ่มแรกสุดเลย การที่เราจะใช้งาน VBA ได้ก็ต้องเปิด Ribbon Developer ขึ้นมาซะก่อน ซึ่งอยู่ใน Excel Option ครับ

จะเข้าไปเขียนโปรแกรมใน VB Editor (หรือ VBE) ก็สามารถกดปุ่ม Visual Basic หรือ Alt+F11 ได้ครับ (ผมกด Alt+F11 ตลอด)

แต่เดี๋ยวเราปิดมันไปก่อนก็ได้ครับ เอาเป็นว่าเปิด VBE ขึ้นมาเป็นแล้วเนอะ กด Alt+F11 นะ

การ Trigger คำสั่งด้วยปุ่ม

จะสั่งให้ Code ทำงานได้ มันต้องมี Trigger หรือ Event บางอย่างเกิดขึ้น ซึ่งตัวที่ใช้บ่อยที่สุดอันนึงก็คือการกดปุ่ม แล้วทำให้เกิด action บางอย่างขึ้นตาม Code ที่เราสั่ง

เรากำลังจะสร้างปุ่มที่ว่านี่แหละ ให้เรากด add button ดังนี้ Developer-> Insert -> Form Control -> Button

จากนั้นวาดรูปสี่เหลี่ยมลงไปในที่ว่างๆ ที่ต้องการให้เกิดปุ่มขึ้น

จากนั้นมันจะเข้าสู่ VBE ภายใต้ Sub ที่ชื่อว่า Test ตามชื่อ Macro ที่เราเขียนเลย

เมื่อเรากด Alt+F11 เพื่อเข้าสู่ VBE หรือกดสร้างปุ่มแล้ว เราก็จะเขียน code ได้

การ Assign ค่า และเรื่องของตัวแปร

ก่อนจะเขียน Code ผมอยากให้เพื่อนๆ เข้าใจ Concept สำคัญในการเขียนโปรแกรมอันนึง ก็คือการใช้เครื่องหมาย = หรือ assignment operator ว่า เราสามารถเอาค่าไปใส่ให้กับตัวแปร หรือใส่ให้กับ Object ต่างๆ ที่เราต้องการได้

โดยรูปแบบคือให้ตัวด้านซ้าย มีค่าเท่ากับตัวด้านขวา

Object = ค่า
ตัวแปร = ค่า

เช่น

x=10

แปลว่าให้ตัวแปร x มีค่าเป็น 10

แล้วถ้าเราเขียนต่อไปอีกบรรทัดว่า

x=x+1

แม้จะดูผิดหลักคณิตศาสตร์ แต่เครื่องหมาย = ในที่นี้ไม่ใช่เครื่องหมายเปรียบเทียบในคณิตศาสตร์ แต่ในการเขียนโปรแกรม มันคือเครื่องหมายที่ใช้ในการกำหนดค่า (assignment operator) ที่ให้ตัวด้านซ้าย มีค่าเท่ากับตัวด้านขวา แล้ว Code ที่เราเขียนก็จะถูก Run จากบนลงล่างไปเรื่อยๆ

ดังนั้นความหมายของ x=x+1 คือให้เอาค่าเดิมใน x (ซึ่งคือ 10) ไปบวก1 จะได้ 11 แล้วใส่แทนค่าลงไปในตัวแปร x

ดังนั้น x จะมีค่าใหม่เป็น 11 นั่นเอง

การอ้างอิง Cell/Range โดยใช้ Range

ภายใน Sub Test() เราสามารถเขียน Code ในรูปแบบ Range(“Cell Reference”) เพื่ออ้างอิง Cell/Range ได้ เช่น

Range("A1")=10

แปลว่า ให้ช่อง A1 มีค่าเป็น 10

พอเขียนเสร็จก็กดปิด VBE ไปก่อนได้เลย แล้วลองไปกดปุ่มที่เราสร้างดูซะ

จากนั้น Code ที่เราเขียนก็จะถูก Run ทำให้ช่อง A1 มีเลข 10 อยู่

นอกจากจะอ้างอิง Cell เดียวแล้วยังใส่เป็นช่วงก็ได้ เช่น

Range("A1:C3") = "Cat"

สังเกตุว่า Cat ใส่อยู่ในเครื่องหมายคำพูด เพราะเป็น Text นะครับ

นอกจากนี้ถ้าเรามีการตั้งชื่อ Defined Name ไว้ เราก็สามารถเอามาใช้ใน Range นี้ได้ด้วย เช่น

Range("ชื่อที่ตั้งไว้") = 999

ซึ่งในชีวิตจริง เราจะใช้การอ้างอิงด้วยชื่อบ่อยมาก เพื่อป้องกันปัญหาการ แทรก/ลบ cell จนตำแหน่งช่องเปลี่ยนไปจนใน Code ไม่ตรงกับความเป็นจริง

การใช้ Message Box

เราสามารถให้ VBA สร้างผลลัพธ์เป็น Message Box เด้งขึ้นมาได้ ด้วยคำสั่งง่ายๆ ว่า MsgBox(“คำที่ต้องการ”) เช่น

MsgBox ("สวัสดี")

การใช้เครื่องหมาย & เชื่อมข้อความ

เราสามารถใช้เครื่องหมาย & มาเชื่อมข้อความ เพื่อผสมรวมคำจากค่าคงที่เช่น “สวัสดี” กับการอ่านค่าจาก Cell ช่อง B1 ที่ผมตั้งชื่อไว้ว่า FirstNameได้ เช่น

ที่สวัสดีอยู่ในเครื่องหมายคำพูด เพราะเป็น Text ส่วน inputName เป็นชื่อตัวแปร ไม่ต้องอยู่ในเครื่องหมายคำพูดนะครับ

Sub Test()
inputName = Range("FirstName")
MsgBox ("สวัสดี " & inputName)

End Sub
Excel VBA พื้นฐาน

การใช้ IF กำหนดเงื่อนไข

เราสามารถเขียน if ใน VBA ได้ในรูปแบบของ

If เงื่อนไข Then
    ทำอะไรถ้าเงื่อนไขเป็นจริง
Else
    ทำอะไรถ้าเงื่อนไขเป็นเท็จ
End If

เช่น

Sub Test()
inputName = Range("FirstName")

If inputName = "ศิระ" Then
    MsgBox ("สวัสดีนายท่าน")
Else
    MsgBox ("ยินดีที่ได้รู้จักครับ " & inputName)
End If
End Sub

ถ้า inputName ที่กรอกมาใน Range ที่ชื่อ FirstName เป็นคำว่า ศิระ ก็จะบอกว่า สวัสดีนายท่าน นอกนั้นก็จะบอกว่า ยินดีที่ได้รู้จักครับ แล้วตามด้วยชื่อ

สังเกตว่า ใช้ inputName = “ศิระ” ได้ เพราะมันรู้ว่าเครื่องหมาย = อันนี้คือเครื่องหมายเปรียบเทียบ ไม่ใช่ assignment แบบตอนที่เขียนส่งค่าให้ตัวแปรตามปกติ

การ Save ไฟล์

ที่สำคัญ เวลาจะ Save ไฟล์ต้อง Save นามสกุล .xlsm, xlsb, xls เท่านั้นนะครับ เพราะถ้าดันไป save เป็น xlsx ปกติล่ะก็… code ที่เราเขียนจะหายไปหมดเลย ดังนั้นอย่าพลาดเด็ดขาดเลยนะ เดี๋ยวนั่งร้องไห้ไม่รู้ด้วย!

สรุป VBA พื้นฐาน ตอนที่ 1

เอาล่ะ บทนี้ของ่ายๆ แค่นี้ก่อนนะครับ ยังไงก็ลองไปเล่นให้ชินมือเข้าไว้นะ ในบทต่อไปจะเป็นการอธิบาย Concept สำคัญใน VBA นั่นก็คือเรื่องของ Object นั่นเอง ซึ่งจะเป็นพื้นที่ไปสู่ บทที่ 3 ที่จะพูดถึงเรื่องการวน Loop ซึ่งคือหัวใจที่สำคัญที่สุดของการเขียนโปรแกรมล่ะ

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

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

  • Facebook
  • YouTube

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

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

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

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

  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI
  • วิธีกำหนดท่าทางแบบให้ได้ดั่งใจด้วย ControlNet ใน Stable Diffusion [Part4]
  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง

บทความแนะนำ

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

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย 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 inventory len link logic lookup match m code merge mid overview paste pivot power query right row solver sort speed split substitute table text time tips trim vba vlookup

Archives

  • April 2023 (3)
  • 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