Forem

Long Nguyễn Xuân
Long Nguyễn Xuân

Posted on

Tạo mục lục trong excel và Personal macro workbook

bài gồm 2 phần

  1. Tạo mục lục trong excel bằng VBA
  2. Personal macro workbook

tạo mục lục trong excel bằng VBA

1. Alt+F11 > Insert New Module
2. copy&paste code vba bên dưới bỏ vào  
3. Alt+F8 > tận hưởng
Enter fullscreen mode Exit fullscreen mode
Sub CreateTableOfContents()
    Dim ws As Worksheet
    Dim tocSheet As Worksheet
    Dim rowNum As Integer
    Dim linkCell As Range

    ' Check if "Table of Contents" sheet already exists, delete it if so
    On Error Resume Next
    Set tocSheet = ThisWorkbook.Worksheets("Table of Contents")
    If Not tocSheet Is Nothing Then
        Application.DisplayAlerts = False
        tocSheet.Delete
        Application.DisplayAlerts = True
    End If
    On Error GoTo 0

    ' Add a new sheet for the TOC
    Set tocSheet = ThisWorkbook.Worksheets.Add
    tocSheet.Name = "Table of Contents"

    ' Add header
    With tocSheet
        .Cells(1, 1).Value = "Table of Contents"
        .Cells(1, 1).Font.Bold = True
        .Cells(1, 1).Font.Size = 14
    End With

    ' Initialize row number for TOC entries
    rowNum = 3

    ' Loop through all sheets and add to TOC
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Table of Contents" Then
            tocSheet.Cells(rowNum, 1).Value = ws.Name
            tocSheet.Hyperlinks.Add Anchor:=tocSheet.Cells(rowNum, 1), _
                Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
            rowNum = rowNum + 1
        End If
    Next ws

    ' Auto-fit columns
    tocSheet.Columns("A:A").AutoFit

    ' Return to the TOC sheet
    tocSheet.Activate

    MsgBox "Table of Contents created successfully!", vbInformation
End Sub
Enter fullscreen mode Exit fullscreen mode

Personal Macro Workbook

vì việc gửi 1 cái file excel có chứa VBA, đôi khi không được phép (vì vấn đề bảo mật), khách hàng họ sẽ không được phép mở 1 cái file excel có chứa VBA, các hệ thống IT của doanh nghiệp cũng chặn loại file này.

Trong excel có 1 cái rất hay gọi là Personal Macro Workbook PERSONAL.XLSB, chúng ta có thể lưu các đoạn vba thường dùng trong này,

hôm nay (2024-12-16) tìm thử nó, để lưu 1 đoạn macro mới thấy trong Office 365, thì nó không được hiển thị ra, mà phải làm 1 cái mẹo

mẹo "hiển thị PERSONAL.XLSB"

  1. tạo 1 file excel bất kỳ, chọn Files > Options > Customize Ribbon
  2. Checked vào menu Developer
  3. mở Ribbons > Developer > Record Macro
  4. chọn "Store macro in: = Personal Macro Workbook

xem hình
Image description

Image description

Image description

Image description

OK giờ nó đã hiển thị ra rồi, lưu vba cần dùng vào đây thôi.

Image description

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay