简体   繁体   English

在不受保护的工作表中实施代码VBA Excel

[英]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. 当我尝试执行此代码时,当工作表不受保护时,它仅在Exit Sub之后执行该部分,仅执行foreach循环。 I think it is caused by the Exit Sub . 我认为这是由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. 我的问题是,我想在工作表受到保护时( for each cell in Range("B6:B112.. )中的for each cell in Range("B6:B112..执行一个不同的代码,而在工作表不受保护时,执行另一种代码(从Dim rng as Range ..开始)我尝试了If..Then..Else但这不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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