ไปยังเนื้อหาหลัก

วิธีทำตัวหนาเฉพาะบรรทัดแรกหรือคำแรกในเซลล์ใน Excel

ในแผ่นงาน Excel อาจมีเซลล์จำนวนมากที่มีหลายบรรทัดซึ่งถูกย่อหน้าด้วยปุ่ม Alt + Enter ในบางกรณีคุณอาจต้องทำตัวหนาเฉพาะบรรทัดแรกในเซลล์เหล่านี้ หรือเฉพาะคำแรกตัวหนาเพื่อให้โดดเด่นในเซลล์ บทความนี้แสดงวิธีการสองวิธีเพื่อให้คุณบรรลุรายละเอียด

เป็นตัวหนาเฉพาะบรรทัดแรกในเซลล์ด้วยรหัส VBA เป็นตัวหนาเฉพาะคำแรกในเซลล์ด้วยรหัส VBA


เป็นตัวหนาเฉพาะบรรทัดแรกในเซลล์ด้วยรหัส VBA

รหัส VBA ต่อไปนี้สามารถช่วยให้คุณเป็นตัวหนาเฉพาะบรรทัดแรกในเซลล์ที่เลือกได้อย่างรวดเร็ว กรุณาดำเนินการดังนี้

1 กด อื่น ๆ + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคลิก สิ่งที่ใส่เข้าไป > โมดูล. จากนั้นคัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่างโมดูล

รหัส VBA: เป็นตัวหนาเฉพาะบรรทัดแรกในเซลล์

Option Explicit
Sub BoldFirstLine()
Dim xRng As Range, xCell As Range
Dim xFirstRow As String
On Error Resume Next
Set xRng = Application.InputBox("Please select range:", "Kutools for", Selection.Address, , , , , 8)
If xRng Is Nothing Then Exit Sub
On Error Resume Next
For Each xCell In xRng
    With xCell
        .Characters(1, InStr(.Value, Chr(10))).Font.Bold = True
    End With
Next
End Sub

3 กด F5 กุญแจสำคัญในการเรียกใช้รหัส จากนั้นก Kutools สำหรับ Excel กล่องโต้ตอบจะปรากฏขึ้นโปรดเลือกช่วงที่มีบรรทัดแรกที่คุณต้องทำให้เป็นตัวหนาจากนั้นคลิกที่ OK ปุ่ม

จากนั้นคุณจะเห็นบรรทัดแรกทั้งหมดของเซลล์ที่เลือกเป็นตัวหนาทันทีตามภาพด้านล่างที่แสดง


เป็นตัวหนาเฉพาะคำแรกในเซลล์ด้วยรหัส VBA

ตามที่แสดงภาพหน้าจอด้านล่างบางครั้งคุณต้องทำตัวหนาเฉพาะคำแรกในช่วง A2: A4 ใน Excel คุณสามารถบรรลุได้ดังต่อไปนี้ทีละขั้นตอน

1 กด อื่น ๆ + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคลิก สิ่งที่ใส่เข้าไป > โมดูล. จากนั้นคัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่างโมดูล

รหัส VBA: เป็นตัวหนาเฉพาะคำแรกในเซลล์

Sub boldtext()
Dim xRng As Range, xCell As Range
On Error Resume Next
Set xRng = Application.InputBox("Please select range:", "Kutools fro Excel", Selection.Address, , , , , 8)
If xRng Is Nothing Then Exit Sub
On Error Resume Next
For Each xCell In xRng
  If xCell.Value <> "" Then
    xCell.Characters(1, InStr(1, xCell.Value, " ") - 1).Font.Bold = True
  End If
Next
End Sub

3 กด F5 กุญแจสำคัญในการเรียกใช้รหัส ในการโผล่ขึ้นมา Kutools สำหรับ Excel กล่องโต้ตอบเลือกช่วงที่คุณต้องการทำให้คำแรกเป็นตัวหนาจากนั้นกดปุ่ม OK ปุ่ม. ดูภาพหน้าจอ:

จากนั้นคุณจะเห็นคำแรกของเซลล์ที่เลือกทั้งหมดเป็นตัวหนาทันทีตามภาพด้านล่างที่แสดง


บทความที่เกี่ยวข้อง:

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
Comments (11)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Thank you! Works perfect.
This comment was minimized by the moderator on the site
Hello,

How to bold only the Last line of the cell?
This comment was minimized by the moderator on the site
Hi Spulber,
The following VBA code can help you bold only the last line of each cell in the selected range.
Please note that if a cell has only one line (i.e., no line breaks), then the entire cell content will be bolded. If a cell is empty or contains only numerical values, this subroutine will not affect it.

Option Explicit
'Updated by Extendoffice 20230721
Sub BoldLastLine()
    Dim xRng As Range, xCell As Range
    Dim xLastLineStart As Long
    Dim xTextLength As Long
    
    On Error Resume Next
    Set xRng = Application.InputBox("Please select range:", "Kutools for Excel", Selection.Address, , , , , 8)
    
    If xRng Is Nothing Then Exit Sub
    On Error GoTo 0
    
    For Each xCell In xRng
        With xCell
            xTextLength = Len(.Value)
            xLastLineStart = InStrRev(.Value, Chr(10)) + 1
            If xLastLineStart > xTextLength Then xLastLineStart = 1
            .Characters(xLastLineStart, xTextLength).Font.Bold = True
        End With
    Next
End Sub
This comment was minimized by the moderator on the site
This is fantastic, thanks so much!
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi. I have many cells that contain multiple lines which were paragraphed by Alt+Enter. I would like to bold and change the color of the first word of each line. Can you help please?
This comment was minimized by the moderator on the site
Hi I have quite a few lines in a cell . I want for make 5th line as bold and italics in a cell . The below code makes only the first line . Can you help

Sub bold()


Dim r As Range, c As Range
Dim ws As Worksheet

Set ws = ActiveSheet
Set r = ws.Range("Y:Y")
For Each c In r
With c
.Font.bold = False
.Value = .Text
.Characters(1, InStr(.Text, vbLf) - 1).Font.bold = True
End With

Next c


End Sub
This comment was minimized by the moderator on the site
How about if I want the second line to be bold?
This comment was minimized by the moderator on the site
Good Day!
If you want to bold only the second line of the cell, please try the following VBA code:

Sub BoldSecondLine()
Dim xRng As Range, xCell As Range
Dim xArr
On Error Resume Next
Set xRng = Application.InputBox("Please select range:", "Kutools for", Selection.Address, , , , , 8)
If xRng Is Nothing Then Exit Sub
For Each xCell In xRng
If xCell <> "" Then
With xCell
xArr = Split(xCell, Chr(10))
.Characters(InStr(.Value, Chr(10)) + 1, Len(xArr(1))).Font.Bold = True
End With
End If
Next
End Sub
This comment was minimized by the moderator on the site
Hi, How about if I want the first three words to be bold?
This comment was minimized by the moderator on the site
Good Day,
Please try below VBA script.

Sub boldtext()
Dim xRng As Range, xCell As Range
Dim xNum As Long, xCount As Long
Dim I As Long, J As Long
Dim xArr
Dim xArrChr10
On Error Resume Next
Set xRng = Application.InputBox("Please select range:", "Kutools fro Excel", Selection.Address, , , , , 8)
If xRng Is Nothing Then Exit Sub
For Each xCell In xRng
xNum = 0
xCount = 0
xArrChr10 = Split(xCell.Value, Chr(10))
For I = 0 To UBound(xArrChr10)
xArr = Split(xArrChr10(I))
For J = 0 To UBound(xArr)
If xArr(J) <> "" Then
xCount = xCount + 1
If xCount > 3 Then Exit For
End If
xNum = xNum + Len(xArr(J)) + 1
Next
Next
xCell.Characters(1, xNum).Font.Bold = True
Next
End Sub
This comment was minimized by the moderator on the site
HI, how about if i want the first two lines to be bold?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations