VBA Excel替代锁定属性?

时间:2013-01-11 12:51:04

标签: excel vba excel-vba

请您告诉我已锁定属性的替代方法。

实际上我有一些宏,这些宏我锁定一些单元格并根据用户环境解锁相同的单元格,但是客户端希望它作为共享工作簿,以便多个用户可以一次编辑。锁定需要取消保护保护,这在工作簿共享时无法实现。

Offo!那么请你给我一个替代我的问题。
任何帮助都会受到很大的赞赏。

2 个答案:

答案 0 :(得分:0)

如果锁定属性仍然可用,则可以选择锁定的单元格。这不会阻止一个坚定的用户,因为他们可能只是禁用宏,但我想锁定更多是为了阻止对公式和东西的意外编辑?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Cr As Range
Dim Rr As Range
If Not Target.Rows.Count > 1 And Not Target.Columns.Count > 1 Then
    If Target.Locked = True Then
        Target.Offset(ColumnOffset:=1).Select
    End If
Else
    If Target.Cells.Count > 1000 Then
        Debug.Print "Selection too big!"
        Target.Cells(1,1).Select
        Exit Sub
    End If
    For Each Cr In Target.Cells
        If Cr.Locked = False Then
            If Rr Is Nothing Then
                Set Rr = Cr
            Else
                Set Rr = Application.Union(Rr, Cr)
            End If
        End If
    Next Cr
    If Not Rr Is Nothing Then
        Rr.Select
    End If
End If
End Sub

当用户选择单个单元格并且其锁定属性为true时,它会将它们移动到下一个未锁定列。如果是选定区域,则会从该区域中删除所有锁定的单元格。我没有真正测试过它,它可能不是最好的解决方案:)。

答案 1 :(得分:0)

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cell As Range 
Dim Rng As Range 
Dim Flag As Boolean 

Flag = False 

For Each Rng In Target.Cells 

    For Each cell In Worksheets("DATA").Range("A1:AZ10") 
        If cell.Address = Rng.Address Then 
            MsgBox "You are not authorized to edit cells in this range." 
            Application.EnableEvents = False 
            Application.Undo 
            Application.EnableEvents = True 
            Flag = True 
            Exit For 
        End If 
    Next cell 

    If Flag = True Then 
        Exit For 
    End If 

Next Rng 

End Sub

Atlast我得到了回答我的问题..