2016-02-27 167 views
1

我正在使用VBA代碼來獲取總和,但單元格被鎖定時它不起作用。更改鎖定單元格

我希望沒有人能夠手動更改特定單元格的值,但VBA代碼可以更改該值。

我的代碼是

Sub Get_Sum() 
    Dim LastRow As Long 
    LastRow = Range("B5000").End(xlUp).Row 
    Range("D" & LastRow + 1) = "Total Amount" 
    Range("E" & LastRow + 1).Formula = "=SUM(E4:E" & LastRow & ")" 
    Range("F" & LastRow + 1).Formula = "=SUM(F4:F" & LastRow & ")" 
    Range("G" & LastRow + 1).Formula = "=SUM(G4:G" & LastRow & ")" 
End Sub 

其中細胞E(E列)被鎖定用密碼。

+0

解鎖片材上執行代碼之前,然後再把鎖定。 http://www.mrexcel.com/archive/VBA/7134.html –

回答

2

也許你可以這樣來做:

Sub Get_Sum() 
    Dim LastRow As Long 
    Sheets("Sheetname").Unprotect Password:= "password" 
    LastRow = Range("B5000").End(xlUp).Row 
    Range("D" & LastRow + 1) = "Total Amount" 
    Range("E" & LastRow + 1).Formula = "=SUM(E4:E" & LastRow & ")" 
    Range("F" & LastRow + 1).Formula = "=SUM(F4:F" & LastRow & ")" 
    Range("G" & LastRow + 1).Formula = "=SUM(G4:G" & LastRow & ")" 
    Sheets("Sheetname").Protect Password:= "password" 
End Sub