[英]Nothing happens when macro is executed
我有一個鎖定的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
這段代碼實際上在我家里的計算機上工作,但是在我工作的計算機上,它執行宏,但是什么也沒有改變,所有單元格/整個工作表都被鎖定,但范圍K1:K372卻沒有。
有人知道家中發生了什么事或與工作有何不同?
非常感謝!
如果我的問題正確無誤,則需要在活動工作表上進行選擇,如果條件滿足,則需要執行一些操作。 對? 但是您的“直到循環”正確嗎?
ActiveSheet.Unprotect "password" Range("B" & rowtolock & ":G" & rowtolock).Select Selection.Locked = False
當您這樣調用時(Range(“ B”&rowtolock&“:G”&rowtolock).Select),我希望活動單元格在工作表中發生變化。 然后下一次迭代將檢查已選擇代碼的單元格。沒有選中您所選擇的單元格(可能是您的家庭工作表滿足條件:只是一個猜測!)
最好的辦法是調試循環並檢查ActiveCell值。 謝謝 !!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.