Thep Excel

Text.Replace – แทนที่ข้อความ

แทนที่ข้อความเก่าด้วยข้อความใหม่ทั้งหมด

Text.Replace ค้นหาทุกส่วนของข้อความเก่า (old) ที่พบในข้อความหลัก (text) และแทนที่ด้วยข้อความใหม่ (new) เป็น Case Sensitive หากต้องการแทนที่แบบ Case Insensitive ต้องใช้ Text.ReplaceAll ร่วมกับ Comparer.OrdinalIgnoreCase

=Text.Replace(text as nullable text, old as text, new as text) as nullable text

By ThepExcel AI Agent
3 December 2025

Syntax & Arguments

=Text.Replace(text as nullable text, old as text, new as text) as nullable text

Argument Type Required Default Description
text text Yes ข้อความหลักที่ต้องการแก้ไข
old text Yes ข้อความย่อยที่ต้องการค้นหาและแทนที่
new text Yes ข้อความใหม่ที่จะนำมาแทนที่

How it works

แก้ไขรหัสสินค้า

เปลี่ยนรหัสสินค้าจาก "PROD-" เป็น "ITEM-"

ทำความสะอาดข้อมูล

แทนที่คำผิด หรือสัญลักษณ์พิเศษ เช่น "#N/A" ด้วยค่าว่าง

สร้าง URL ที่ถูกต้อง

แทนที่ช่องว่างในชื่อไฟล์ด้วยเครื่องหมายขีดกลาง (-) สำหรับ URL

Examples

ตัวอย่างที่ 1: แทนที่ส่วนของคำ

ค้นหา "pp" ใน "apple" แล้วแทนที่ด้วย "rr"
Power Query Formula:

=Text.Replace("apple", "pp", "rr")

Result:

arrle

ตัวอย่างที่ 2: แทนที่ทั้งคำ

แทนที่คำว่า "World" ด้วย "Power Query"
Power Query Formula:

=Text.Replace("Hello World", "World", "Power Query")

Result:

Hello Power Query

ตัวอย่างที่ 3: แทนที่หลายครั้ง

แทนที่ตัวอักษร 'a' ทุกตัวด้วย 'o'
Power Query Formula:

=Text.Replace("banana", "a", "o")

Result:

bonono

FAQs

Text.Replace เป็น Case Sensitive หรือไม่?

เป็น Case Sensitive ครับ เช่น Text.Replace(“Apple”, “apple”, “Orange”) จะไม่แทนที่ ถ้าต้องการแทนที่แบบไม่สนใจ Case ต้องใช้ Text.ReplaceAll ร่วมกับ Comparer.OrdinalIgnoreCase

ถ้าต้องการแทนที่แค่ครั้งแรกที่เจอทำอย่างไร?

Text.Replace จะแทนที่ทุกครั้งที่เจอครับ ถ้าต้องการแทนที่แค่ครั้งแรก ต้องใช้ฟังก์ชันที่ซับซ้อนขึ้น หรือใช้ Text.RemoveRange ร่วมกับ Text.Insert

Resources & Related

Additional Notes

ฟังก์ชัน Text.Replace ใน Power Query ใช้สำหรับแทนที่ข้อความย่อย (Substring) ที่พบในข้อความหลัก ด้วยข้อความใหม่ โดยจะแทนที่ทุกครั้งที่พบ

Leave a Reply

Your email address will not be published. Required fields are marked *