Apply a Macro to a list of sheets

Fener

New Member
Joined
Feb 9, 2015
Messages
4
Hi, i'm sorry for my bad english, but i'm Italian.
I would like to apply a macro to a list of sheets.
I mean, I have 30 sheets, and the their names are written in the sheet "Start", Range("A5:A100"). In a sheet "Setting" i have the macro that I would like to apply to every sheets (and the specific name of each sheet is in that range)...
How can I do it?

Thank you very much
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Code:
Sub macro(sheetName As String)

    Sheets(sheetName).Select
    
    'HERE IS WHERE YOUR MACRO GOES


End Sub


Sub populateList()


    For x = 5 To 100
        Call macro(Cells(x, 1).Text)
    Next x


End Sub

You could do something like that so you don't have to re-write the macro?
 
Upvote 0
I tried, but it's not working...
I would like the macro read each company in the sheet "Start", and for each company, it enters in the specific sheet's name and apply the macro that comes from sheet "setting".
I would like to do in this way in order that if I want to modify some formula, I will work only in the sheet "Setting" (as test) and after I will apply with the macro, the same layout/formula/setting of the sheet "setting" to the List of sheets contained in the sheet "start"

I hope I explained a bit better.

Thank you for the help
 
Upvote 0
I have moved this thread to "questions in other languages". Please re-state your question in Italian and please clarify a little more the third paragraph of your most recent post:
I would like to do in this way in order that if I want to modify some formula, I will work only in the sheet "Setting" (as test) and after I will apply with the macro, the same layout/formula/setting of the sheet "setting" to the List of sheets contained in the sheet "start"

Ho spostato la discussione alle "domande in altre lingue". Inserisci nuovamente indicare la tua domanda in italiano e vi prego di chiarezza un po 'di più, terzo comma, del più recente post:
Mi piacerebbe fare in questo modo, in modo che se voglio modificare una formula, lavorerò solo in il foglio "Setting" (come test) e dopo io applicherò con la macro, il layout stesso / formula / impostazione del foglio "impostazione" per la lista di fogli contenuti nel foglio "start"
 
Last edited:
Upvote 0
Ho 500 fogli:
"Start" contiene una lista di diverse società (più di 500)
"Setting" è un foglio test in cui scrivo e testo la macro che poi applicherò agli altri fogli, i cui nomi sono contenuti nel foglio "start"

Quello che vorrei fare è:
Per ogni nome di foglio contenuto in "Start", applicare la macro (testata nel foglio "Setting") ad ogni specifico nome/foglio contenuto nella lista.
 
Upvote 0
<font face="Consolas," "lucida="" sans="" typewriter",="" console",="" "courier="" new",="" monospace="">This is a very basic version of a macro that would do the type of activity you are describing. Note that it does not have any kind of error handling. So if a sheet is missing, the execution will stop and you will be thrown into the debugger.


Questa è una versione di base di una macro che avrebbe fatto il tipo di attività che si sta descrivendo. Si noti che non ha alcun tipo di gestione degli errori. Quindi, se un foglio manca, l'esecuzione si ferma e si sarà gettato nel debugger.


<font face=Consolas, "Lucida Sans Typewriter", "Lucida Console", "Courier New", Monospace><SPAN style="color:#00007F">Sub</SPAN> Example1()<br><br>    <SPAN style="color:#007F00">'// extremely basic</SPAN><br><br>    <SPAN style="color:#00007F">Dim</SPAN> celItem     <SPAN style="color:#00007F">As</SPAN> Excel.Range, _<br>        wsTarget    <SPAN style="color:#00007F">As</SPAN> Excel.Worksheet<br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> celItem <SPAN style="color:#00007F">In</SPAN> Worksheets("Start").Range("A5:A500").Cells<br>        <br>        <SPAN style="color:#00007F">Set</SPAN> wsTarget = Worksheets(celItem.Value)<br>        <br>        RepeatedProcedure wsTarget<br>        <br>    <SPAN style="color:#00007F">Next</SPAN> celItem<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> RepeatedProcedure(<SPAN style="color:#00007F">ByRef</SPAN> wsTarget <SPAN style="color:#00007F">As</SPAN> Excel.Worksheet)<br><br>    <SPAN style="color:#007F00">'// This would be the procedure that you wish to</SPAN><br>    <SPAN style="color:#007F00">'// execute for each worksheet on the list.</SPAN><br>    <SPAN style="color:#007F00">'</SPAN><br>    <SPAN style="color:#007F00">'// For demonstration purposes this routine just</SPAN><br>    <SPAN style="color:#007F00">'// changes the color of the first four cells.</SPAN><br>    <br>    <SPAN style="color:#00007F">With</SPAN> wsTarget.Range("A1:B2").Interior<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> .Color<br>            <SPAN style="color:#00007F">Case</SPAN> vbCyan:    .Color = vbYellow<br>            <SPAN style="color:#00007F">Case</SPAN> vbYellow:  .Color = vbGreen<br>            <SPAN style="color:#00007F">Case</SPAN> vbGreen:   .Color = vbRed<br>            <SPAN style="color:#00007F">Case</SPAN> vbRed:     .Color = vbBlue<br>            <SPAN style="color:#00007F">Case</SPAN> Else:      .Color = vbCyan<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Last edited:
Upvote 0
We can improve on this slightly by adding some code that will keep execution from stopping if there is no sheet on the list.


Possiamo migliorare questo leggermente aggiungendo qualche codice che manterrà esecuzione di arrestarsi se non c'è foglio sulla lista.


<font face=Consolas, "Lucida Sans Typewriter", "Lucida Console", "Courier New", Monospace><br><SPAN style="color:#00007F">Sub</SPAN> Example2()<br><br>    <SPAN style="color:#007F00">'// code so that simple error don't stop execution.</SPAN><br><br>    <SPAN style="color:#00007F">Dim</SPAN> celItem     <SPAN style="color:#00007F">As</SPAN> Excel.Range, _<br>        wsTarget    <SPAN style="color:#00007F">As</SPAN> Excel.Worksheet<br>    <br>    <SPAN style="color:#007F00">'// we're simply going to ignore any errors.</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> celItem <SPAN style="color:#00007F">In</SPAN> Worksheets("Start").Range("A5:A500").Cells<br>        <br>        <SPAN style="color:#007F00">'// we need to set the target worksheet to nothing.</SPAN><br>        <SPAN style="color:#007F00">'// if we don't and we error out, we'll run our</SPAN><br>        <SPAN style="color:#007F00">'// repeated macro a second time on the same worksheet.</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wsTarget = <SPAN style="color:#00007F">Nothing</SPAN><br>        <br>        <SPAN style="color:#007F00">'// no sense doing anything if the cell is empty</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Len(celItem.Value) <> 0 <SPAN style="color:#00007F">Then</SPAN><br>            <br>            <SPAN style="color:#00007F">Set</SPAN> wsTarget = Worksheets(celItem.Value)<br>        <br>            <SPAN style="color:#007F00">'// if the sheet exists then our object</SPAN><br>            <SPAN style="color:#007F00">'// variable is set, otherwise it is NOTHING.</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> wsTarget <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> RepeatedProcedure wsTarget<br>            <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#007F00">'// (len(cell) <> 0)</SPAN><br>                <br>    <SPAN style="color:#00007F">Next</SPAN> celItem<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Or we can even add in a little error handling that will mark those cells that contain invalid names.


Oppure possiamo anche aggiungere in un po 'di gestione degli errori che segnerà quelle celle che contengono i nomi validi.


<font face=Consolas, "Lucida Sans Typewriter", "Lucida Console", "Courier New", Monospace><br><SPAN style="color:#00007F">Sub</SPAN> Example3()<br><br>    <SPAN style="color:#007F00">'// do more than ignore errors, mark them.</SPAN><br>    <br>    <SPAN style="color:#00007F">Dim</SPAN> celItem     <SPAN style="color:#00007F">As</SPAN> Excel.Range, _<br>        cmtError    <SPAN style="color:#00007F">As</SPAN> Excel.Comment, _<br>        strComment  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>        wsTarget    <SPAN style="color:#00007F">As</SPAN> Excel.Worksheet<br>    <br>    <SPAN style="color:#007F00">'// we're simply going to ignore any errors.</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrorHandler<br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> celItem <SPAN style="color:#00007F">In</SPAN> Worksheets("Start").Range("A5:A500").Cells<br>        <br>        <SPAN style="color:#007F00">'// we need to set the target worksheet to nothing.</SPAN><br>        <SPAN style="color:#007F00">'// if we don't and we error out, we'll run our</SPAN><br>        <SPAN style="color:#007F00">'// repeated macro a second time on the same worksheet.</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wsTarget = <SPAN style="color:#00007F">Nothing</SPAN><br>        <br>        <SPAN style="color:#007F00">'// no sense doing anything if the cell is empty</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Len(celItem.Value) <> 0 <SPAN style="color:#00007F">Then</SPAN><br>            <br>            <SPAN style="color:#00007F">Set</SPAN> wsTarget = Worksheets(celItem.Value)<br>        <br>            <SPAN style="color:#007F00">'// if the sheet exists then our object</SPAN><br>            <SPAN style="color:#007F00">'// variable is set, otherwise it is NOTHING.</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> wsTarget <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> RepeatedProcedure wsTarget<br>            <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#007F00">'// (len(cell) <> 0)</SPAN><br>                <br>EndOfLoop:<br>    <SPAN style="color:#00007F">Next</SPAN> celItem<br>    <br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br>    <br>ErrorHandler:<br><SPAN style="color:#007F00">'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨</SPAN><br>    <br>    <SPAN style="color:#00007F">With</SPAN> celItem<br>    <br>        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> cmtError = .Comment<br>        <br>        <SPAN style="color:#007F00">'// If the cell doesn't have a comment, we'll add a comment.</SPAN><br>        <SPAN style="color:#007F00">'// If it DOES already have a comment, we'll tack our text onto the</SPAN><br>        <SPAN style="color:#007F00">'// end of the existing comment.</SPAN><br>        <br>        <SPAN style="color:#00007F">If</SPAN> cmtError <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            .AddComment Text:="This worksheet does not exist."<br>        <SPAN style="color:#00007F">Else</SPAN><br>            <SPAN style="color:#00007F">Let</SPAN> strComment = cmtError.Text<br>            cmt<SPAN style="color:#00007F">Error</SPAN>.Text vbCrLf & vbCrLf & "This worksheet does not exist.", Len(strComment) + 4<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <br>        <SPAN style="color:#007F00">'// rather than actually overwriting any existing cell formatting, we</SPAN><br>        <SPAN style="color:#007F00">'// will add a conditional format that will turn the cell pink.</SPAN><br>        <SPAN style="color:#007F00">'// We can very easily go in and delete this by hand.  Much easier</SPAN><br>        <SPAN style="color:#007F00">'// to clean up rather than trying to undo formatting applied directly</SPAN><br>        <SPAN style="color:#007F00">'// to the cell.</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> .FormatConditions<br>            .Add Type:=xlExpression, Formula1:="=TRUE"<br>            <SPAN style="color:#00007F">With</SPAN> .Item(.Count)<br>                .SetFirstPriority<br>                .StopIfTrue = True<br>                <SPAN style="color:#00007F">With</SPAN> .Interior<br>                    .PatternColorIndex = xlAutomatic<br>                    .Color = 10066431<br>                    .TintAndShade = 0<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN> <SPAN style="color:#007F00">'// .interior</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN> <SPAN style="color:#007F00">'// fc.item(n)</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN> <SPAN style="color:#007F00">'// format conditions</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN> <SPAN style="color:#007F00">'// cell item</SPAN><br>    <br>    <SPAN style="color:#00007F">On</SPAN> Error <SPAN style="color:#00007F">GoTo</SPAN> ErrorHandler<br>    <SPAN style="color:#00007F">Resume</SPAN> EndOfLoop<br>    <br>End <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
"Setting" è un foglio test in cui scrivo e testo la macro che poi applicherò agli altri fogli, i cui nomi sono contenuti nel foglio "start"


The way you originally wrote this in English, it can be interpreted to mean that "Setting" is a macro sheet, something that was in Excel about twenty years ago. However, the XLM 4.0 macro language was replaced with VBA back in 1997. I assume you did NOT mean this is a macro sheet, but that you mean something else. However I am unclear as to your meaning.


Il modo in cui è stato originariamente scritto questo in inglese, può essere interpretato nel senso che "Setting" è un foglio macro, qualcosa che era in Excel una ventina di anni fa. Tuttavia, il linguaggio macro XLM 4.0 è stato sostituito con VBA nel 1997. Presumo che non intendevo questo è un foglio di macro, ma che dire qualcosa di diverso. Comunque io sono poco chiaro per il vostro senso.
 
Upvote 0

Forum statistics

Threads
1,215,685
Messages
6,126,201
Members
449,298
Latest member
Jest

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top