执行macros时没有任何反应

我有一个locking的Excel工作表,我想当用户激活工作表时,解锁完成条件的单元格的范围(即,如果单元格A1> 0,则解锁范围A2:A5)。

另一方面,我也希望解锁范围K1:K372。

我已经有这个代码:

' macro activates by activating the Worksheet Private Sub Worksheet_Activate() ' macro activates by activating the Worksheet ' Set a counter to iterate through all the rows where to if the cell accomplishes the condition Dim rowtolock As Integer rowtolock = 5 Do Until IsEmpty(ActiveCell) 'select the first cell where I will check if the value is greater or less than 0 Range("M" & rowtolock).Select ' if the seleted cell value is greater or equal than 0, unprotect the sheet (because it´s protectet), select the range I want to unlock so that the user can makes changes JUST on those cells, unlock them, and finally lock anotherway the sheet. If ActiveCell.Value >= 0 Then ActiveSheet.Unprotect "password" Range("B" & rowtolock & ":G" & rowtolock).Select Selection.Locked = False Selection.FormulaHidden = False ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End If ' add 1 to the counter, so that the next selected cell is one row below rowtolock = rowtolock + 1 Loop 'unprotect sheet, unlock range, and protect sheet ActiveSheet.Unprotect "password" Range("K1:K372").Select Selection.Locked = False Selection.FormulaHidden = False ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub 

这段代码实际上在我家里的计算机上工作,但在我的电脑上工作,它执行macros,但没有任何变化,所有单元格/整个工作表被locking,但没有范围K1:K372

有人知道在家里发生了什么事情,或者在工作中会有什么不同?

非常感谢!

如果我的问题是正确的,则需要在活动工作表上进行select,如果条件满足,则需要执行一些操作。 对? 但是你的直到循环是否正确?

ActiveSheet.Unprotect "password" Range("B" & rowtolock & ":G" & rowtolock).Select Selection.Locked = False

当你这样调用(Range(“B”&rowtolock&“:G”&rowtolock).Select)时,我希望工作表中的活动单元格发生变化。 然后下一次迭代将检查所select的单元格的代码。不是你select的一个(可能是你的家庭工作表满足条件:只是一个猜测!)

最好的事情是debugging你的循环,并检查ActiveCell值。 谢谢 !!!