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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,223,785
Messages
6,174,539
Members
452,571
Latest member
MarExcelTips

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