Hoppa till huvudinnehåll

Hur kör jag samma makro på flera kalkylblad samtidigt i Excel?

Normalt kan vi köra ett makro i ett kalkylblad. Om det finns flera ark behöver du använda samma makro, bör du utlösa koden ett och ett ark. Om det finns något annat snabbt sätt att köra samma makro på flera kalkylblad samtidigt i Excel?

Kör eller kör samma makro på flera kalkylblad samtidigt med VBA-kod


Kör eller kör samma makro på flera kalkylblad samtidigt med VBA-kod

För att köra ett makro på flera ark samtidigt utan att utlösa det ett och ett ark kan du använda följande VBA-kod, gör så här:

1. Håll ner ALT + F11 nycklar för att öppna Microsoft Visual Basic för applikationer fönster.

2. Klicka Insert > Modulernaoch klistra in följande makro i Modulerna Fönster.

VBA-kod: Kör samma makro på flera kalkylblad samtidigt:

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

Anmärkningar: I ovanstående kod, kopiera och klistra in din egen kod utan Sub rubrik och End Sub sidfot mellan Sub Runcode () och End Sub skript. Se skärmdump:

doc kör makro i alla ark 1

3. Sätt sedan markören vid det första delmakrot och tryck på F5 för att köra koden och din makrokod kommer att tillämpas på ett och ett ark.


Ta bort alla makron från flera arbetsböcker:

Kutools för Excel's Batch Ta bort alla makron verktyget kan hjälpa dig att ta bort alla makron från flera arbetsböcker efter behov. Ladda ner och testa gratis Kutools för Excel nu!

doc kör makro om cell ändras 3

Kutools för Excel: med mer än 300 praktiska Excel-tillägg, gratis att prova utan begränsning på 30 dagar. Ladda ner och testa gratis nu!

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
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