Macro - Loop through all sheets, except for sheet names containing certain word

L

Legacy 220867

Guest
Hi all, can someone please help me with this one. I have a macro that loops through all the sheets in my workbook and then executes a certain code. However I do not want certain sheets to be included. How can I alter the code to skip sheets containing the word 'Database'?

Code:
<code>
Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer

WS_Count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_Count

'My Code

Next I

End Sub
</code>
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,

Perhaps try something like this:

Code:
Sub WorksheetLoop()

Dim ws  As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If InStr("Database", ws.Name) = 0 Then
        ' your code here
    End If
Next ws

End Sub
 
Upvote 0
Hello, I combined both of your formulas however my code doesn't work. When I run it first time its running but not doing anything actually. When I run it second time, It's not working and excel shows me "Paste method of Worksheet class failed"

Code:
ub copy_remainingsheets()
    
    Dim ws  As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        If InStr("Database", ws.Name) = 0 Then
            ' your code here
            Dim WS_Count As Integer
            Dim I As Integer
        
            ' Set WS_Count equal to the number of worksheets in the active
            ' workbook.
             WS_Count = ActiveWorkbook.Worksheets.Count

             ' Begin the loop.
             For I = 4 To WS_Count

                ' Insert your code here.
            
                Range("A23").Select
                Range(Selection, Selection.End(xlDown)).Select
                Range(Selection, Selection.End(xlToRight)).Select
                Application.CutCopyMode = False
                Selection.Copy
                Sheets("new database").Select
                Range("A1").Select
                Selection.End(xlDown).Select
                ActiveCell.Offset(1).Select
                ActiveSheet.Paste
            Next I
                                                                                                                                                                                
        End If
    Next ws
    
End Sub

What I am trying to do here? I made macro where I downloaded several web pages to separate sheets in one workbook. Right now I would like to take certain information from this sheets to one sheet. Those informations are in exactly same location. And I would like to delete each sheet where I obtained and copy information.
 
Upvote 0
I would just add some additional information to my previous post, which are recomended in Guidelines for posting.

I have win 7. service pack 1. I am using Excel 2010.

I am new to VBA and learning from videos and some webpages. I think than mistake is some where in loop and copy paste phase.

If I should provide some additional information please let me know and I ll try to answer.
 
Upvote 0

Forum statistics

Threads
1,217,384
Messages
6,136,274
Members
450,001
Latest member
KWeekley08

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