Automatically translated.View original post

🔻 Auto Adapt Excel Column with View Code (VBA)

2025/12/21 Edited to

... Read moreถ้าใครเริ่มหัดใช้ VBA ใน Excel แล้วเจอปัญหา “คอลัมน์กว้างไม่พอดี” โดยเฉพาะไฟล์ที่มีหัวตารางอย่าง วันที่, รายชื่อลูกค้า, รายการสินค้า, จำนวน, ราคา พอมีข้อมูลยาวๆ เข้ามาทีไรต้องมานั่งดับเบิลคลิกปรับความกว้างตลอด บอกเลยว่าแก้ด้วย VBA ได้ง่ายมาก และเป็นงานอัตโนมัติที่คุ้มสุดๆ แนวคิดของโค้ดนี้คือให้ Excel “ปรับคอลัมน์ให้พอดีกับข้อความ” ทุกครั้งที่เราเข้าไปที่ชีทนั้น (Activate) ทำให้เปิดชีทเมื่อไหร่ ตารางจะดูเรียบร้อยทันที ไม่ต้องกด AutoFit เอง วิธีใช้งาน (ทำครั้งเดียวแล้วจบ) 1) ไปที่แท็บชีทด้านล่าง (เช่น Sheet1) แล้วคลิกขวา 2) เลือก View Code 3) จะเปิดหน้าต่าง Microsoft Visual Basic for Applications ขึ้นมา ให้เลือกออบเจ็กต์ของชีทนั้น 4) วางโค้ดในช่องว่างของชีท: Private Sub Worksheet_Activate() Columns.AutoFit End Sub ทิปที่ผมใช้จริงแล้วช่วยมาก - ถ้าชีทมีหลายตารางและไม่อยากให้ทั้งชีท AutoFit ทั้งหมด (บางทีทำให้คอลัมน์แคบ/กว้างเกิน) ให้จำกัดช่วงคอลัมน์ เช่น A:E Private Sub Worksheet_Activate() Columns("A:E").AutoFit End Sub - ถ้ามีคอลัมน์ “ราคา/จำนวน” ที่อยากให้คงความกว้างเท่าๆ กัน ไม่อยากให้ AutoFit แล้วแกว่งไปมา ให้ AutoFit เฉพาะคอลัมน์ข้อความ เช่น B:D แล้วล็อกคอลัมน์ตัวเลขไว้ Private Sub Worksheet_Activate() Columns("B:D").AutoFit Columns("E:F").ColumnWidth = 12 End Sub ข้อควรระวังเล็กน้อย - ไฟล์ต้องบันทึกเป็น .xlsm (Macro-Enabled) ไม่งั้นโค้ดจะไม่ทำงาน - ถ้าข้อมูลในชีทเยอะมากๆ การ AutoFit ทุกครั้งที่เข้าอาจหน่วงนิดนึง แนะนำจำกัดช่วงคอลัมน์ที่จำเป็น - ถ้าเปิดไฟล์แล้ว Macro ถูกปิด (Disable) โค้ดจะไม่รัน ต้องกด Enable Content ก่อน โดยรวมถือเป็น VBA ใน Excel ที่เหมาะกับมือใหม่มาก เพราะสั้น เข้าใจง่าย และเห็นผลทันที โดยเฉพาะงานตารางขาย/สต็อก/รายชื่อลูกค้า ที่ข้อมูลเปลี่ยนทุกวัน

Related posts

100 Excel Tips
100 Excel Tips The best Shortcuts, Formulas, Formatting & Advanced Tips Bookmark this, you'll save 1,000 of hours! A list of tips that all professionals need. Here is a preview: ⌨️ Shortcuts 1. How to hide a column? Ctrl + 0 2. How to hide a row? Ctrl + 9 3. How to
ExpressoSolut

ExpressoSolut

1452 likes

Excel vs Word
Excel formatting does the same thing as many people using Word #tutorial #exceltips #exceltricks #exceltutorial #exceltraining #worktips #alwayslearning
Excel tips at XecuteTheVision

Excel tips at XecuteTheVision

4948 likes

Excel Guide
#excelspreadsheets #
FWSbeautybar

FWSbeautybar

6 likes

An outdoor dining scene with people seated at tables, featuring the overlaid text "Freeze the Top Row in Excel." A purple glass is visible in the foreground on a black mesh table.
A light beige background with black text asking, "Do you ever have hundreds of rows of data and when scrolling down, you loose sight of the column header and forget which column/field is which?"
A light beige background with black text stating, "This is when freezing your top row is crucial! I will show you how!"
HOW I FREEZE THE TOP ROW IN EXCEL
The ability to freeze that first row of my column headers has been a lifesaver, allowing me to scroll through thousands of rows of data without losing sight of my column headers/fields. By simply selecting the row below the one you want to freeze, heading to the "View" tab, and clicking
Peyton’s Place

Peyton’s Place

105 likes

A brown note card with 'Best study HABITS to adapt' written on it, placed over a background of handwritten study notes on a grid paper.
A close-up of a planner showing a 'TO DO' list with checked tasks, emphasizing the importance of 'Consistency' in study habits.
A desk setup with a laptop displaying historical text about colonial trade and a notebook with handwritten notes, illustrating 'Active engagement' in studying.
5 study habits to adapt!
Here are five of the best study habits to adopt: 1. Consistency Regular, consistent study sessions over time are more effective than cramming. Set aside dedicated time each day or week for studying. 2. Active engagement Actively engage with the material by summarizing, asking questions
essynotes

essynotes

268 likes

Why AI will replace creators who don’t adapt & how
1. Use AI to research trending problems in your niche. 2. Create a quick product (guide, template) with AI. 3. Post your findings + solution as content. 4. Offer your product + invite people to learn AI through your affiliate link. You’re the one teaching the new tools, not getting repl
Her Confident Era

Her Confident Era

3 likes

LOOKING STRONG IN MY NEW ADAPT.
Hard work pays off → this is your sign to trust the process. Flaws? Embracing them. Progress? Celebrating it. Strength? Built daily. Your body adapts. Your mind transforms. Keep showing up. \ #NewAdapt \ #GluteGains \ #GymProgress \ #Lemon8Fitness \#StrongNotSkinn
Emma

Emma

6 likes

Adapt & Overcome: Side-Lying Leg Raises 💪🏾
Who says an injured arm can stop us? 💪🏾 Staying active is all about adapting. These Side-Lying Leg Raises are perfect for building core and glute strength without needing upper-body support. 👉🏾 How to do it: 1️⃣ Lie on your side with legs straight and your injured arm resting comfortably. 2️⃣
DevDad

DevDad

0 likes

Healthy Habits To Adapt
#healthyhabits #lemon8challenge #healthylifestyle2024 #habits #jesuslovesyou
Korpo

Korpo

16 likes

🐠 ADAPT TO SURVIVE
Nature teaches a powerful lesson: survival isn’t about strength alone. It’s about adapting when life changes. The people who grow the most are often the ones who learn to adjust the fastest. #LifeRulers #AdaptAndGrow #NatureWisdom #MindsetMatters #RuleYourLife
Life Rulers 👑

Life Rulers 👑

11 likes

Viajar no es perder el ENFOQUE es ADAPTARTE.
🍽️ HOW I STAY CONSISTENT WHILE TRAVELING ✈️ Protein • Fiber • Whole Foods Travel doesn’t mean falling off track, it means making intentional choices. Here’s what helped me stay consistent on this trip: 🥩 Protein first at every meal 🥦 Fiber whenever possible (veggies, fruit, whole foods
Meli|Women’s Fitness Coach

Meli|Women’s Fitness Coach

3 likes

Adapt with intention.
ChangeDrTiff

ChangeDrTiff

4 likes

Adapt, Change, Live 🌸
This months' Patreon/Happy Mail Club exclusive art is all about adaptation, the shedding of habits and traits both unique and inherited that no longer serve who you are and the world you're in. #darkacademia #hopecore #paleoart #contemporarynaturalism
Stacia

Stacia

12 likes

Embracing chaos and learning to adapt 🛠️💗
Girl, let me tell you about starting over and the beautiful chaos that comes with it 💪🏻✨ Moving this fall was supposed to be smooth - new furniture ordered, friends lined up to help, everything planned perfectly. But life had other ideas. 💡🚛🍁📦👯‍♀️ Furniture deliveries? Delayed. Move date? Pus
MoonlightRein

MoonlightRein

12 likes

UNWINDING HABITS TO ADAPT
Wellness Habits 🧘 Habit 1: Drinking hot tea 🧘 How It Helps Me: relaxes my mind, incorporates antioxidants 🧘 Habit 2: Eating balanced meals and working out and staying active 🧘 How It Helps Me: Activates oxytocin, adrenaline, and works muscles 🧘 Habit 3: Running a hot bath 🧘 How It Helps Me: R
EntrepreneurTay

EntrepreneurTay

10 likes

SUMMER BODY TIPS - ADAPT & CHANGE ♠️
Follow @Capital Gentlemen for DAILY science based 🧬🧠 FITNESS 🔱♠️ - Build Muscle & Build Momentum 📈 - Possible goals 👇 - Compound Movements - 5-10 pounds per 1-2 weeks Isolation movements 2.5 pounds to 5 pounds per 1-2 weeks - With consistent time under tension & form 👆 - “E
Capital Gentlemen | Fitness

Capital Gentlemen | Fitness

4 likes

Empaths aren't weak. They're wired to adapt.
Empaths don't lose themselves because they're weak. They lose themselves because they're wired to adapt. 💜 Empaths naturally adjust to the people around them. They read the room. They feel what others feel. They make space for everyone else's experience. In a healt
Melissa | Torch & Soul

Melissa | Torch & Soul

79 likes

Your body is meant to adapt
With much love and acceptance, Tor #summerbod #write a thank you to my body #bodylove #embracevulnerability
torasavage_

torasavage_

12 likes

See more