简体   繁体   中英

implement code in unprotected and protected sheets VBA Excel

when I am trying to execute this code it only executes the part after Exit Sub , only the foreach loop is executed, when the sheet is unprotected. I think it is caused by the Exit Sub . My problem is that I want to execute to different codes one when the sheet is protected ( for each cell in Range("B6:B112.. )) and the other(starting at Dim rng as Range ..) when the sheet is unprotected. I tried If..Then..Else but that does not work.

Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("test").ProtectContents Then Exit Sub

 For Each cell In Range("B6:B112")
 If cell.Value <> "" Then
 cell.EntireRow.Hidden = False
 Else
 cell.EntireRow.Hidden = True
 End If
 Next cell

 Dim rng As Range
 Dim eingabeNr As Double, letzteZeile As Long, eingabeDatum As String, eingabeNrString As String
 Set rng = Range("D:BC")

THX.

Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("test").ProtectContents = True Then 'added = true for readability.

   For Each cell In Range("B6:B112")
       If cell.Value <> "" Then
           cell.EntireRow.Hidden = False
       Else
           cell.EntireRow.Hidden = True
       End If
   Next cell
Else
   Dim rng As Range
   Dim eingabeNr As Double, letzteZeile As Long, eingabeDatum As String,_
   eingabeNrString As String
   Set rng = Range("D:BC")

   Do some stuff here

End if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM