🔧 Excel VBA Tip: Auto Column Sizing Easy
🔧 Excel VBA Tip: Resize Auto Columns and Rows Easy in One Click! 📊
Have you ever encountered a problem where data in Excel is cut or not displayed in full columns or rows? 🤔
Sometimes when we work with data in Excel, our tables may seem out of order, some data are cut off, or all data can be displayed in the cell. 📉 This is a problem we often meet, but don't worry, because today we have a command in the VBA that will help you solve it easily. 🎯
🔧 Use Cells. EntireColumn. AutoFit.
This command will help resize the width of all columns to fit the data in the longest cell, so you no longer have to resize yourself to hassle!
🔧 Use Cells. EntireRow. AutoFit.
If you have all the messages in the rows you want to display, this command will help you adjust the height of the rows to fit the data in each row perfectly!
- Try it out. - Yes, and your schedule will look more beautiful and organized. 📊✨
# ExcelVBA # AutoFit # ExcelTips # OfficeHacks # DataManagement # ExcelLover # VBAProgramming
ถ้าคุณเจอปัญหา “คอลัมน์แคบ ข้อความโดนตัด” บ่อยๆ (โดยเฉพาะตารางพวก Order ID, Customer Name, Product, Category, Quantity, Sales Amount, Date) เราว่าใช้ Excel VBA ทำปุ่ม AutoFit ไว้คือคุ้มมาก เพราะจัดตารางให้พอดีข้อมูลได้ทันที ไม่ต้องลากขอบคอลัมน์ทีละอัน โค้ดพื้นฐานที่เราใช้ประจำ (ปรับทั้งคอลัมน์และแถวในชีตที่กำลังเปิดอยู่) Sub AutoFit_All() Cells.EntireColumn.AutoFit Cells.EntireRow.AutoFit End Sub แต่ในงานจริง บางทีเราไม่อยากให้ AutoFit ทั้งชีต เพราะทำให้ไฟล์หนัก/ช้า หรือบางคอลัมน์ไม่เกี่ยวข้อง แนะนำให้จำกัด “ช่วงข้อมูล” ก่อน เช่น ถ้าตารางเริ่มที่ A1 และมีข้อมูลต่อเนื่อง: Sub AutoFit_UsedRange() With ActiveSheet.UsedRange .Columns.AutoFit .Rows.AutoFit End With End Sub กรณีทำรายงานที่ต้อง AutoFit เฉพาะบางคอลัมน์ (เช่น B=Customer Name, C=Product, D=Category) ก็ระบุคอลัมน์ไปเลย: Sub AutoFit_SpecificColumns() Columns("B:D").AutoFit End Sub ทริคที่เราเจอแล้วช่วยได้เยอะ: ถ้าในเซลล์มีการขึ้นบรรทัดใหม่/Wrap Text (เช่นชื่อสินค้า/รายละเอียด) ให้เปิด Wrap Text แล้วค่อย AutoFit แถว จะได้ความสูงพอดีจริงๆ Sub WrapAndAutoFitRows() With ActiveSheet.UsedRange .WrapText = True .Rows.AutoFit End With End Sub วิธีใช้งานแบบเร็วที่สุด: 1) กด Alt+F11 เปิด VBA Editor 2) Insert > Module 3) วางโค้ดที่เลือกใช้ 4) กลับ Excel แล้วกด Alt+F8 เลือกแมโครเพื่อรัน ถ้าคุณทำงานกับตารางขาย/สต็อกบ่อยๆ เราแนะนำให้เซฟแมโครนี้ไว้ใน Personal Macro Workbook ด้วย จะได้ใช้ AutoFit Columns/Rows ได้กับทุกไฟล์ทันที เปิดไฟล์ไหนมาก็จัดตารางให้สวยเป็นระเบียบได้ในคลิกเดียว

























































































