![]() |
Macro for several worksheets
I have a spreadsheet with several workbooks. I would
like to be able to use a macro to delete certain data in all my worksheets (there are about 25). I have created a macro to delete the data in just one worksheet but cannot figure out how to do it in more than one. Thanks, Jdp |
Macro for several worksheets
Sub Macro1()
For Each ws In Worksheets ws.Select Your code here Next ws End Sub -----Original Message----- I have a spreadsheet with several workbooks. I would like to be able to use a macro to delete certain data in all my worksheets (there are about 25). I have created a macro to delete the data in just one worksheet but cannot figure out how to do it in more than one. Thanks, Jdp . |
Macro for several worksheets
Hi JDP,
you may use Sub macro2() Dim i As Integer For i = 1 To 25 Sheets(i).Select 'your code here Next End Sub But for this you really need to know that your sheets are in the right sequence. Safer is: make a list of all the sheet-names you want to use, e.g. putting them in a table from A1 to A25 and name this range "MySheets" Then the macro would be Sub Macro1() Dim rngName As Range For Each rngName In Range("MySheets") Sheets(rngName.Value).Select 'Your code here Next rngName End Sub Best regards Wolf -----Original Message----- Thank you very much, but won't this delete all my sheets? I need it to stop after the 25th sheet (or only do the first 25. -----Original Message----- Sub Macro1() For Each ws In Worksheets ws.Select Your code here Next ws End Sub -----Original Message----- I have a spreadsheet with several workbooks. I would like to be able to use a macro to delete certain data in all my worksheets (there are about 25). I have created a macro to delete the data in just one worksheet but cannot figure out how to do it in more than one. Thanks, Jdp . . . |
Macro for several worksheets
Thanks Wolf. I think we are getting close. Right now I
am getting a "Compile Error. Wrong number of arguments or invalid property assignment" It doesn't like the Sheets parameter in the macro. Any suggestions? -----Original Message----- Hi JDP, you may use Sub macro2() Dim i As Integer For i = 1 To 25 Sheets(i).Select 'your code here Next End Sub But for this you really need to know that your sheets are in the right sequence. Safer is: make a list of all the sheet-names you want to use, e.g. putting them in a table from A1 to A25 and name this range "MySheets" Then the macro would be Sub Macro1() Dim rngName As Range For Each rngName In Range("MySheets") Sheets(rngName.Value).Select 'Your code here Next rngName End Sub Best regards Wolf -----Original Message----- Thank you very much, but won't this delete all my sheets? I need it to stop after the 25th sheet (or only do the first 25. -----Original Message----- Sub Macro1() For Each ws In Worksheets ws.Select Your code here Next ws End Sub -----Original Message----- I have a spreadsheet with several workbooks. I would like to be able to use a macro to delete certain data in all my worksheets (there are about 25). I have created a macro to delete the data in just one worksheet but cannot figure out how to do it in more than one. Thanks, Jdp . . . . |
All times are GMT +1. The time now is 01:57 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com