+ Reply to Thread
Results 1 to 18 of 18

Macro deactivated in specific worksheet

Hybrid View

graiggoriz Macro deactivated in specific... 07-17-2014, 09:36 AM
nathansav Re: Macro deactivated in... 07-17-2014, 09:37 AM
graiggoriz Re: Macro deactivated in... 07-17-2014, 12:03 PM
Norie Re: Macro deactivated in... 07-17-2014, 12:25 PM
Arkadi Re: Macro deactivated in... 07-17-2014, 12:55 PM
graiggoriz Re: Macro deactivated in... 07-18-2014, 04:08 AM
graiggoriz Re: Macro deactivated in... 07-18-2014, 04:24 AM
nathansav Re: Macro deactivated in... 07-18-2014, 04:35 AM
HaHoBe Re: Macro deactivated in... 07-18-2014, 04:46 AM
graiggoriz Re: Macro deactivated in... 07-21-2014, 02:26 AM
mikerickson Re: Macro deactivated in... 07-21-2014, 02:33 AM
graiggoriz Re: Macro deactivated in... 07-23-2014, 03:21 AM
Arkadi Re: Macro deactivated in... 07-23-2014, 09:29 AM
graiggoriz Re: Macro deactivated in... 09-21-2014, 04:15 PM
HaHoBe Re: Macro deactivated in... 09-21-2014, 11:45 PM
graiggoriz Re: Macro deactivated in... 09-21-2014, 02:50 PM
HaHoBe Re: Macro deactivated in... 09-21-2014, 03:21 PM
Arkadi Re: Macro deactivated in... 09-22-2014, 01:25 PM
  1. #1
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Macro deactivated in specific worksheet

    Hello,

    Would it be possible not to run a macro in specific worksheet?? I run a macro with the shortcut CTRL + P and I need to have that macro to be deactivated on certain worksheets (the worksheet names are "RENTCU1", "RENTCU2", "RENTCU3", "RENTCU4".

    Thanks,
    Graig

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro deactivated in specific worksheet

    put an activesheet.name check at the start of the macro.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Thanks but I need to have it work the other way around. I would like to specify the worksheets I do not want to macro to be run.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Macro deactivated in specific worksheet

    Does this macro run on multiple worksheets when you call it or only on the active sheet?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro deactivated in specific worksheet

    can you post your macro? it is hard to know how to help with the code if we don't know what we are helping on. Do you reference the worksheet in your macro? Does the macro loop through all sheets?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  6. #6
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Sure!

    My macro is that one:

    Sub Macro1()
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
    
    breaks_count = sh.HPageBreaks.Count
    
    For i = 1 To breaks_count
        If rng Is Nothing Then Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7) Else Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
    Next
        
    If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        
    'PRINT VERSION
    
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
        
    End Sub
    The worksheets that I do not want that macro o be run are: "RENTCU1", "RENTCU2", "RENTCU3", "RENTCU4"

    Many Thanks in advance.
    Graig

  7. #7
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Sure!

    My macro is that one:

    Sub Macro1()
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
    
    breaks_count = sh.HPageBreaks.Count
    
    For i = 1 To breaks_count
        If rng Is Nothing Then Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7) Else Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
    Next
        
    If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        
    'PRINT VERSION
    
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
        
    End Sub
    The worksheets that I do not want that macro o be run are: "RENTCU1", "RENTCU2", "RENTCU3", "RENTCU4"

    Many Thanks in advance.
    Graig

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro deactivated in specific worksheet

    Yes, thats what i was getting at, in the 1st line of macro have

    if activesheet.name<>"Barred Sheet1" and activesheet.name<>"Barred Sheet2" then
    
         your macro
    
    
    end if

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro deactivated in specific worksheet

    Hi, graiggoriz,

    maybe add this to ThisWorkbook and see if it works as expected:
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Select Case ActiveSheet.Name
      Case "RENTCU1", "RENTCU2", "RENTCU3", "RENTCU4"
        Cancel = True
      Case Else
    End Select
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  10. #10
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Hello Holger,

    Are you saying that I need to *** that code in the 4 worksheets? (right click on the tab; view code. and add the code?

    If so, I already have the below code in each worksheet. If you meant to add the code differently can you please provide me the step? I don't know how to do that...

    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
      If ws.Name <> "CURRENT1" Then ws.Visible = xlSheetVeryHidden
    Next ws
    End Sub
    Many Thanks,
    Graig

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro deactivated in specific worksheet

    I think this will do what you want.
    Sub Macro1a()
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    If Not (sh.Name Like "RENTCU[1234]") Then
    
        sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
        
        breaks_count = sh.HPageBreaks.Count
        
        For i = 1 To breaks_count
            If rng Is Nothing Then Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7) Else Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
        Next
            
        If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
            
        'PRINT VERSION
        
            ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
    End If
    
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  12. #12
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Hey mikerickson!! You got it!! Very good.

    I only have on problem with the printing part as if I cancel the printing I see the macro and in the file I use there is password! Would you know if the below macro can be enhanced and give me a pop up "would you like to print that document" and putting a condition that if "no" then it ends the macro instead of entering in debug mode! In the end I need to print that document as pdf but I did not know how to do that so I recorded a macro (CTRL + P)

    Do you think you could help me with that matter?


       'PRINT VERSION
        
            ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
    Many Thanks,
    Graig

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro deactivated in specific worksheet

    graig, change it to:

    'PRINT VERSION
    toprint = MsgBox("Would you like to print?", vbYesNo)
    If toprint = vbYes Then
         ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
    End If

  14. #14
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Hello,

    Thanks to all! I have compiled the macros and got that:

    Sub PRIINT()
    
    
    ActiveSheet.Unprotect Password:="p@ssw0rd!"
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    'ADD NEW LINE FOR NOT RUNNING THE MACRO IN CASE SHEET EQUAL TO CURRENT1,2,3,4,5,6:
    
    If Not (sh.Name Like "CURRENT[1234]") Then
    
    
    sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
    
    breaks_count = sh.HPageBreaks.Count
    
    For i = 1 To breaks_count
        If rng Is Nothing Then Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7) Else Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
    Next
        
    If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        
    ActiveSheet.Protect Password:="p@ssw0rd!"
    
    'PRINT VERSION
    toprint = MsgBox("Would you like to print?", vbYesNo)
    If toprint = vbYes Then
         ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
    End If
    
    End Sub
    I have the following error:
    Compile error:
    Block If without End If

    Can you please help me fixing that error?

    Many thanks,
    Graig

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro deactivated in specific worksheet

    Hi, Graig,

    I don´t get the reason for repeating the post from 08:50 PM at 10:15 PM (I know there have been Forum issues and my post wasn´t properly displayed nor passed to my count nor was displayed on the survey of the Forum). Did you try my suggestion?

    Ciao,
    Holger

  16. #16
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Re: Macro deactivated in specific worksheet

    Hello,

    Thanks to all! I have compiled the macros and got that:

    Sub PRIINT()
    
    
    ActiveSheet.Unprotect Password:="p@ssw0rd!"
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    'ADD NEW LINE FOR NOT RUNNING THE MACRO IN CASE SHEET EQUAL TO CURRENT1,2,3,4,5,6:
    
    If Not (sh.Name Like "CURRENT[1234]") Then
    
    
    sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
    
    breaks_count = sh.HPageBreaks.Count
    
    For i = 1 To breaks_count
        If rng Is Nothing Then Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7) Else Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
    Next
        
    If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        
    ActiveSheet.Protect Password:="p@ssw0rd!"
    
    'PRINT VERSION
    toprint = MsgBox("Would you like to print?", vbYesNo)
    If toprint = vbYes Then
         ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
    End If
    
    End Sub
    I have the following error:
    Compile error:
    Block If without End If

    Can you please help me fixing that error?

    Many thanks,
    Graig

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro deactivated in specific worksheet

    Hi, graiggoriz,

    indention could be one way to find the error, this is untested (I shifted some of the lines):
    Sub PRIINT()
    
    Dim rng As Range, sh As Worksheet, breaks_count As Long, i As Long
     
    Set sh = ActiveSheet
    
    'ADD NEW LINE FOR NOT RUNNING THE MACRO IN CASE SHEET EQUAL TO CURRENT1,2,3,4,5,6:
    
    If Not (sh.Name Like "CURRENT[1234]") Then
      sh.Unprotect Password:="p@ssw0rd!"
      sh.Range("a" & sh.ListObjects(1).ListRows.Count).Activate
      breaks_count = sh.HPageBreaks.Count
      
      For i = 1 To breaks_count
        If rng Is Nothing Then
          Set rng = sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7)
        Else
          Set rng = Union(sh.Range("B" & sh.HPageBreaks(i).Location.Row - 1).Resize(, 7), rng)
        End If
      Next i
          
      If Not rng Is Nothing Then rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
          
      'PRINT VERSION
      If MsgBox("Would you like to print?", vbYesNo) = vbYes Then
        sh.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
      End If
      sh.Protect Password:="p@ssw0rd!"
    End If
    
    Set sh = Nothing
    
    End Sub
    Ciao,
    Holger

  18. #18
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro deactivated in specific worksheet

    your 'print version section has an if, no end if. The end if near the bottom applies to "if not" at the top. I suggest adding an "End If" just before "End Sub"

+ 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. Copying specific data to a specific field in a worksheet macro
    By bradpeh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2013, 01:52 AM
  2. [SOLVED] Delete worksheet if it is deactivated?
    By Alexander_Golinsky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2013, 01:54 AM
  3. Macro to pull specific data from one worksheet to another worksheet
    By Kettie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2013, 05:21 PM
  4. Code adjustment needed-warning message and highlighting is deactivated.
    By baffled1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-01-2011, 09:23 PM
  5. Worksheet_Deactivate procedure leaves me on the deactivated sheet
    By carsto in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-13-2008, 05:21 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