Bỏ qua nội dung chính

Làm cách nào để chạy cùng một macro trên nhiều trang tính cùng một lúc trong Excel?

Thông thường, chúng ta có thể chạy một macro trong một trang tính, nếu có nhiều trang tính cần áp dụng cùng một macro này, bạn nên kích hoạt mã từng trang một. Nếu có bất kỳ cách nhanh chóng nào khác để chạy cùng một macro trên nhiều trang tính cùng một lúc trong Excel?

Chạy hoặc thực thi cùng một macro trên nhiều trang tính cùng lúc với mã VBA


Chạy hoặc thực thi cùng một macro trên nhiều trang tính cùng lúc với mã VBA

Để chạy macro trên nhiều trang tính cùng một lúc mà không cần kích hoạt từng trang một, bạn có thể áp dụng mã VBA sau, vui lòng thực hiện như sau:

1. Giữ ALT + F11 phím để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.

2. Nhấp chuột Chèn > Mô-đunvà dán macro sau vào Mô-đun Cửa sổ.

Mã VBA: Chạy cùng một macro trên nhiều trang tính cùng một lúc:

Sub Dosomething()
    Dim xSh As Worksheet
    Application.ScreenUpdating = False
    For Each xSh In Worksheets
        xSh.Select
        Call RunCode
    Next
    Application.ScreenUpdating = True
End Sub
Sub RunCode()
    'your code here
End Sub

Chú thích: Trong mã trên, vui lòng sao chép và dán mã của riêng bạn mà không có Sub tiêu đề và End Sub chân trang giữa Runcode phụ ()End Sub tập lệnh. Xem ảnh chụp màn hình:

doc chạy macro trong tất cả các trang tính 1

3. Sau đó đặt con trỏ vào macro phần đầu tiên và nhấn F5 để chạy mã và mã macro của bạn sẽ được áp dụng cho từng trang tính.


Xóa tất cả macro khỏi nhiều sổ làm việc:

Kutools cho Excel's Hàng loạt Xóa tất cả Macro tiện ích có thể giúp bạn xóa tất cả các macro khỏi nhiều sổ làm việc khi bạn cần. Tải xuống và dùng thử miễn phí Kutools cho Excel ngay bây giờ!

doc chạy macro nếu ô thay đổi 3

Kutools cho Excel: với hơn 300 bổ trợ Excel tiện dụng, dùng thử miễn phí không giới hạn trong 30 ngày. Tải xuống và dùng thử miễn phí ngay!

Công cụ năng suất văn phòng tốt nhất

🤖 Trợ lý AI của Kutools: Cách mạng hóa việc phân tích dữ liệu dựa trên: Thực thi thông minh   |  Tạo mã  |  Tạo công thức tùy chỉnh  |  Phân tích dữ liệu và tạo biểu đồ  |  Gọi các hàm Kutools...
Các tính năng phổ biến: Tìm, đánh dấu hoặc xác định các bản sao   |  Xóa hàng trống   |  Kết hợp các cột hoặc ô mà không làm mất dữ liệu   |   Vòng không có công thức hữu ích. Cảm ơn !
Siêu tra cứu: Nhiều tiêu chí VLookup    VLookup Nhiều Giá Trị  |   VLookup trên nhiều trang tính   |   Tra cứu mờ ....
Danh sách thả xuống nâng cao: Tạo nhanh danh sách thả xuống   |  Danh sách thả xuống phụ thuộc   |  Danh sách thả xuống nhiều lựa chọn ....
Trình quản lý cột: Thêm một số cột cụ thể  |  Di chuyển cột  |  Chuyển đổi trạng thái hiển thị của các cột ẩn  |  So sánh dãy và cột hữu ích. Cảm ơn !
Các tính năng nổi bật: Tiêu điểm lưới   |  Chế độ xem thiết kế   |   Thanh công thức lớn    Trình quản lý sổ làm việc & trang tính   |  Thư viện tài nguyên (Văn bản tự động)   |  Bảng chọn ngày   |  Kết hợp các bảng tính   |  Mã hóa/Giải mã ô    Gửi email theo danh sách   |  Siêu lọc   |   Bộ lọc đặc biệt (lọc in đậm/nghiêng/gạch ngang...) ...
15 bộ công cụ hàng đầu12 bản văn CÔNG CỤ (thêm văn bản, Xóa ký tự,...)   |   50 + Biểu đồ Các loại (Biểu đồ Gantt,...)   |   40+ Thực tế Công thức (Tính tuổi dựa trên ngày sinh,...)   |   19 chèn CÔNG CỤ (Chèn mã QR, Chèn ảnh từ đường dẫn,...)   |   12 Chuyển đổi CÔNG CỤ (Số thành từ, Chuyển đổi tiền tệ,...)   |   7 Hợp nhất & Tách CÔNG CỤ (Các hàng kết hợp nâng cao, Chia ô,...)   |   ... và nhiều hơn nữa

Nâng cao kỹ năng Excel của bạn với Kutools for Excel và trải nghiệm hiệu quả hơn bao giờ hết. Kutools for Excel cung cấp hơn 300 tính năng nâng cao để tăng năng suất và tiết kiệm thời gian.  Bấm vào đây để có được tính năng bạn cần nhất...

Mô tả


Tab Office mang lại giao diện Tab cho Office và giúp công việc của bạn trở nên dễ dàng hơn nhiều

  • Cho phép chỉnh sửa và đọc theo thẻ trong Word, Excel, PowerPoint, Publisher, Access, Visio và Project.
  • Mở và tạo nhiều tài liệu trong các tab mới của cùng một cửa sổ, thay vì trong các cửa sổ mới.
  • Tăng 50% năng suất của bạn và giảm hàng trăm cú nhấp chuột cho bạn mỗi ngày!
Comments (16)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
The original macro "How to run the same macro on multiple worksheets at same time in Excel" is fantastic for my purposes, thanks so much. I was wondering if there's a way to get it to return to the first worksheet where I have a control to run the macro. Currently it goes through 25 worksheets doing my code (autofit & Sort by date) and stops once it's done the last worksheet of the 25. Returning to the first sheet would be the icing on the cake! Thanks in advance for thinking about this.
This comment was minimized by the moderator on the site
is there a way to only do it on selected worksheets and not all?
This comment was minimized by the moderator on the site
Hello, karen,
To run the code on selected sheet, please apply the below code:
Step1: Copy anf paste the below code into the Module Window
'Replace this code with your own, but keep the script inside the parentheses
Sub LCase(ws As Worksheet)
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    Rng.Value = VBA.LCase(Rng.Value)
Next
End Sub

Sub RunOnSelectedSheets()
    Dim ws As Worksheet
    For Each ws In ActiveWindow.SelectedSheets
        LCase ws    'change the LCase code name to your own code name
    Next ws
End Sub



Step2: Replace the first section of code above with your own code, but keep the script inside the parentheses. And change the LCase code name to your own code name.

Step3: Then, select the worksheet you want to run this code.
After running the code, your code will be executed on each of the selected sheets.

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Terimakasih sebelumnya atas ilmu ilmu yg sudah di share. Saya sudah memakai beberapa VBA anda.

Tapi bolehkan saya meminta tolong untul mencarikan VBA yg tepat untuk mengaktifkan VBA yg sudah ada di sheet tertentu tanpa harus masuk ke sheet tersebut.

Contohnya.
Saya sudah menggunakan vBA anda yg berfungsi untuk mengurutkan data secara otomatis.

Tetapi ketika data yg saya terapkan itu berubah karena hasil pencarian data dari sheet yg berbeda. vBA tersebut tidak aktif. Harus terlebih dahulu masuk ke Sheet Tersebut untuk merubah data dengan manual barulah VBa pengurutan dat tersebut aktif.

Bisakah dicarikan VBA nya?
This comment was minimized by the moderator on the site
It works, but the current sheet is always running twice. The rest sheets are perfect and run only once!
This comment was minimized by the moderator on the site
Hello, hou,
May be you can use the below code to your need:
Sub WorksheetLoop()
         Dim Current As Worksheet
         For Each Current In Worksheets

          'Insert your code here.
           
      End Sub


Note: In the above code, please copy and paste your own code without the Sub heading and End Sub footer.

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Thank you so much! But it's not working. It only runs the current sheet for several times.
This comment was minimized by the moderator on the site
How do you exclude certain sheets and add On Error Resume Next? Thanks in advance
This comment was minimized by the moderator on the site
Before xSh.selectyou have to activate
So,xSh.activatexSh.select
This comment was minimized by the moderator on the site
The code "Run the same macro on multiple worksheets at same time" works perfectly, Thanks !

However I have this error at "xSh.Select" and I don't know why...there is no issue visible in the sheets... AND I don't have any hidden sheets.

The error is '1004': Method'Select' of object '_Worksheet' failed


Thanks in advance for your help
This comment was minimized by the moderator on the site
For those wiFor those with 1004 runtime error, verify you don´t have any hidden sheets. If you do, unhide them. Then this code snippet works. Greetings.th 1004 runtime error, verify you don´t have any hidden sheets. If you do, unhide them. Then this code snippet works. Greetings.
This comment was minimized by the moderator on the site
Is there a way to make this run only on unhidden sheets? I need this code to run only on visible sheets.
This comment was minimized by the moderator on the site
or
On Error Resume Next

'above

xSh.Select

This comment was minimized by the moderator on the site
1004 runtime error
This comment was minimized by the moderator on the site
Sub Dosomething()
Dim xSh As Worksheet
Application.ScreenUpdating = False
For Each xSh In Worksheets
xSh.Select
Call RunCode
Next
Application.ScreenUpdating = True
End Sub
Sub RunCode()
With Worksheets("2018")
.EnableOutlining = True
.EnableSelection = xlNoRestrictions
.Protect Password:="workana1234", _
Contents:=True, UserInterfaceOnly:=True
End With
End Sub


Hola! he seguido las pautras pero me señala como error la fila xSh.Select.

Al dar F5 me indica que se ha producido un error 1004 en tiempo de ejecucion.

Error en el metodo "Select" de Objeto "_Worksheet"



Help!!!!
This comment was minimized by the moderator on the site
On Error Resume Next
above
xSh.Select
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations