VLOOKUP & XLOOKUP
เชื่อมข้อมูลข้ามตาราง
Section titled “เชื่อมข้อมูลข้ามตาราง”เมื่อข้อมูลอยู่คนละตาราง เราต้องใช้ Lookup Functions เพื่อดึงข้อมูลจากตารางหนึ่งมาใช้ในอีกตารางหนึ่ง — เหมือนกับ JOIN ใน SQL
VLOOKUP — ตัวคลาสสิก
Section titled “VLOOKUP — ตัวคลาสสิก”=VLOOKUP(search_key, range, index, [is_sorted])| Parameter | ความหมาย |
|---|---|
| search_key | ค่าที่ต้องการค้นหา |
| range | ตารางที่จะค้น (Column แรกต้องเป็น Key) |
| index | Column ที่ต้องการดึงค่า (นับจาก 1) |
| is_sorted | FALSE = Exact Match, TRUE = Approximate |
ตัวอย่าง
Section titled “ตัวอย่าง”สมมติมี 2 ตาราง:
ตารางคำสั่งซื้อ (Orders):
| Order ID | Product ID | Qty |
|---|---|---|
| 001 | P101 | 5 |
| 002 | P203 | 3 |
ตารางสินค้า (Products):
| Product ID | Name | Price |
|---|---|---|
| P101 | Widget A | 500 |
| P203 | Gadget B | 1200 |
=VLOOKUP(B2, Products!A:C, 2, FALSE)→ ค้นหา Product ID ในตาราง Products → ดึง Name (Column 2)
=VLOOKUP(B2, Products!A:C, 3, FALSE) * C2→ ดึง Price × Qty = ยอดรวม#include <stdio.h>#include <string.h>typedef struct { char id[10]; char name[50]; int price; } Product;
int vlookup(Product *p, int n, const char *key) { for (int i = 0; i < n; i++) if (strcmp(p[i].id, key) == 0) return p[i].price; return -1; // not found}import pandas as pdorders = pd.read_csv("orders.csv")products = pd.read_csv("products.csv")# merge = VLOOKUP equivalentmerged = orders.merge(products, on="Product_ID", how="left")SELECT o.order_id, p.name, p.price * o.qty AS totalFROM orders oJOIN products p ON o.product_id = p.product_id;XLOOKUP — ตัวใหม่ที่ดีกว่า
Section titled “XLOOKUP — ตัวใหม่ที่ดีกว่า”XLOOKUP แก้ปัญหาทุกข้อของ VLOOKUP:
=XLOOKUP(search_key, lookup_range, result_range, [not_found], [match_mode], [search_mode])ข้อดีเหนือ VLOOKUP
Section titled “ข้อดีเหนือ VLOOKUP”| VLOOKUP | XLOOKUP |
|---|---|
| ค้นได้แค่ซ้ายไปขวา | ค้นได้ทุกทิศทาง |
| ใช้ Column Index (ตัวเลข) | ระบุ Range ผลลัพธ์ตรง ๆ |
| Default = Approximate Match | Default = Exact Match |
| ไม่มี Not Found handler | มี parameter สำหรับ Not Found |
ตัวอย่าง
Section titled “ตัวอย่าง”=XLOOKUP(B2, Products!A:A, Products!B:B)→ ค้น Product ID → ดึง Name (ระบุ Column ตรง ๆ)
=XLOOKUP(B2, Products!A:A, Products!B:C)→ ดึงหลาย Column พร้อมกัน (Name + Price)
=XLOOKUP(B2, Products!A:A, Products!B:B, "ไม่พบสินค้า")→ ถ้าค้นไม่เจอ แสดง "ไม่พบสินค้า" แทน Error
=XLOOKUP(B2, Products!B:B, Products!A:A)→ ค้นจาก Name → ดึง Product ID (ขวาไปซ้าย — VLOOKUP ทำไม่ได้!)เปรียบเทียบสูตรเดียวกัน
Section titled “เปรียบเทียบสูตรเดียวกัน”VLOOKUP: =VLOOKUP(B2, Products!A:C, 2, FALSE)XLOOKUP: =XLOOKUP(B2, Products!A:A, Products!B:B)ทั้งคู่ให้ผลเหมือนกัน แต่ XLOOKUP อ่านเข้าใจง่ายกว่า และไม่พังเมื่อเพิ่ม Column
// XLOOKUP ใน C ก็คือ linear search เหมือนกัน// แต่สามารถค้นจาก field ไหนก็ได้const char* xlookup(Product *p, int n, const char *key, int search_field) { for (int i = 0; i < n; i++) { if (search_field == 0 && strcmp(p[i].id, key) == 0) return p[i].name; if (search_field == 1 && strcmp(p[i].name, key) == 0) return p[i].id; } return "Not Found";}# pandas merge ทำได้ทุกทิศทาง# ค้นจาก Name → ดึง ID (reverse lookup)result = products.set_index("Name").loc["Widget A", "Product_ID"]-- SQL JOIN ไม่มีข้อจำกัดเรื่องทิศทางSELECT p.product_id FROM products p WHERE p.name = 'Widget A';Common Pitfalls สรุป
Section titled “Common Pitfalls สรุป”| ปัญหา | สาเหตุ | วิธีแก้ |
|---|---|---|
#N/A Error | ค้นไม่เจอ หรือข้อมูลมี Space ซ่อน | ใช้ TRIM() กับข้อมูล / XLOOKUP มี not_found |
| ได้ค่าผิด | ลืมใส่ FALSE (Exact Match) | ใส่ FALSE ใน VLOOKUP เสมอ |
| สูตรพังเมื่อเพิ่ม Column | Column Index เลื่อน | เปลี่ยนเป็น XLOOKUP |
| ค้นขวาไปซ้ายไม่ได้ | ข้อจำกัดของ VLOOKUP | ใช้ XLOOKUP หรือ INDEX+MATCH |
// Pitfall ใน C: ลืม check return valueint price = vlookup(products, n, "INVALID_ID");if (price == -1) printf("Not found!\n");# Pitfall: merge ได้ NaN ถ้า key ไม่ matchmerged = orders.merge(products, on="Product_ID", how="left")print(merged.isna().sum()) # ตรวจหา missing values-- Pitfall: LEFT JOIN ได้ NULL ถ้าไม่ matchSELECT o.*, COALESCE(p.name, 'Unknown') AS product_nameFROM orders o LEFT JOIN products p ON o.product_id = p.product_id;- VLOOKUP — ใช้งานง่าย แต่มีข้อจำกัด (ค้นซ้ายไปขวาเท่านั้น, ใช้ Column Index)
- XLOOKUP — แนะนำสำหรับงานใหม่ (ค้นได้ทุกทิศ, syntax ชัดเจน, มี not_found)
- ใส่ FALSE เสมอ ใน VLOOKUP เพื่อ Exact Match
- Lookup = JOIN ใน SQL — แนวคิดเดียวกัน ต่างแค่ syntax