执行宏时没有任何反应

时间:2015-04-15 09:42:43

标签: excel vba excel-vba

我有一张锁定的Excel工作表,我希望当工作表被用户激活时,解锁完成条件的单元格区域(即,如果单元格A1> 0,则解锁范围A2:A5)。 / p>

另一方面,我也想要解锁范围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

这段代码实际上可以在家里的电脑上使用,但是在我的电脑上工作时,它会执行宏,但没有任何变化,所有单元格/整张表都被锁定,但不是K1:K372

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

非常感谢!

1 个答案:

答案 0 :(得分:0)

如果我的问题是正确的,你需要在活动表上做出选择,如果条件满足,你需要做点什么。对?但你的Do Until Loop是否正确?

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

当您这样打电话时(范围(" B"& rowtolock&":G"& rowtolock)。选择),我希望工作表中的活动单元格发生变化。然后下一次迭代将检查代码选择单元格。不是你选择的一个(可能是你的家庭工作表满足条件:只是猜猜!)

最好的方法是调试循环并检查ActiveCell值。谢谢!!!