+ Reply to Thread
Results 1 to 14 of 14

Run macro on all sheets except two

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Run macro on all sheets except two

    I must be overlooking something as no matter what I try the Replace function keeps replacing the string on the sheet that I want it to exclude.

    I've tried with and without the quotes around the sheet names. I'm stumped



    Sub ReplacingTest()
    
    Dim Sh As Object
    
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Name <> BackUp And Sh.Name <> Options Then
                
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
                
            End If
        Next Sh
        
     
    End Sub
    Last edited by Vlad717; 10-23-2017 at 08:40 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    Try this where CodeName is the name between parenthesis.

    Sub ReplacingTest()
    
    Dim Sh As Object
    
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.CodeName <> "Sheet1" And Sh.CodeName <> "Sheet2" Then
                
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
                
            End If
        Next Sh
        
     
    End Sub
    Last edited by bakerman2; 10-23-2017 at 09:13 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    Do you mean like so?


    Sub ReplacingTest()
    
    Dim Sh As Object
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.CodeName <> "BackUp" And Sh.CodeName <> "Options" Then
                
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
                
                
            End If
        Next Sh
        
    End Sub

    It still replaces everything in the workbook including the Backup and Options sheet.

  4. #4
    Registered User
    Join Date
    03-16-2013
    Location
    Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Run macro on all sheets except two

    Sub ReplacingTest()

    Dim Sh As Object

    For Each Sh In ActiveWorkbook.Worksheets
    If Sh.Name <> "Options" And Sh.Name <> "BackUp" Then
    Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    End If
    Next Sh

    End Sub

  5. #5
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    Quote Originally Posted by shaiz View Post
    Sub ReplacingTest()
    
    Dim Sh As Object
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Name <> "Options" And Sh.Name <> "BackUp" Then
    
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            
            End If
        Next Sh
    
    End Sub
    I don't believe there is any change in the code from what was initially posted? It will run with or without the quotes okay

  6. #6
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    Gotcha. Appreciate you explaining it. I end up with the below in that case and its replacing everything in the workbook lol

    Sub ReplacingTest()
    
    Dim Sh As Object
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.CodeName <> "Sheet7" Or Sh.CodeName <> "Sheet8" Then
                
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
    
            End If
        Next Sh
        
    End Sub

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    Sorry, my bad. It's the name NOT between parenthesis.

    In this case Blad1
    Attached Images Attached Images

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    Sub ReplacingTest()
    
    Dim Sh As Object
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.CodeName <> "Sheet7" And Sh.CodeName <> "Sheet8" Then
                
                Sh.Cells.Replace What:="VLOOKUP(""19A19"",Options!$F$9:$H$45,3,FALSE)", Replacement:="123456789", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
    
            End If
        Next Sh
        
    End Sub

  9. #9
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    Wops, that was left over from trying different things. I just tried it with 'And' as well and I get the same result

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    This is taking too long, post example file so we can test for ourselves.
    This works without a doubt.

  11. #11
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    I think there is a larger issue at play here. I stripped down the book to nothing but a few borders but the entire sheet structure is still there. Its attached (but somewhat unneeded as its working there). I closed and reopened the example book and it doesn't replace the vlookup string like it is intended... (which is good). I literally copy/paste the code into the exact same module with the exact same sheet names of what was just stripped and it replaces it.... wtf

    Edit. I should note that I did not copy the workbook or copy the sheets. It was only 'save as' and I also tried changing the file name of the problem book to no avail.
    Attached Files Attached Files

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    Next time please post an example file without vba password protection.
    The code posted in Post#6 works just fine in your file so I don't see what more can be done.

  13. #13
    Registered User
    Join Date
    02-09-2013
    Location
    Alberta, CA
    MS-Off Ver
    O365
    Posts
    80

    Re: Run macro on all sheets except two

    Shoot, sorry! I forgot to take that part out.

    Yeah it seems to work fine in a new workbook but for some reason in this one, even in its own module... it just wants to replace everything. Very strange. Appreciate the help though!

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Run macro on all sheets except two

    Without the actual file to test I'm a bit in the dark I'm afraid.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheets.
    By excelfriend1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-22-2016, 10:48 PM
  2. [SOLVED] unlock sheets apply macro then lock sheets again
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2015, 10:21 PM
  3. Replies: 2
    Last Post: 09-19-2013, 04:22 AM
  4. [SOLVED] Macro to Unprotect sheets-unlock range of cells-protect sheets
    By jrace in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2013, 10:45 AM
  5. Creating a macro to print only the sheets and portion of sheets that i select.
    By cadamhill in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2012, 01:48 PM
  6. Replies: 0
    Last Post: 10-10-2012, 08:56 AM
  7. Macro for filter on protected workbook that works for all sheets, no matter what sheets are named?
    By StargateFanFromWork in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2006, 02:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1