+ Reply to Thread
Results 1 to 18 of 18

Macro deactivated in specific worksheet

Hybrid View

  1. #1
    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

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

    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
    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

+ 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