programing

vba를 사용하여 엑셀에서 데이터를 잠그는 방법

showcode 2023. 6. 14. 22:01
반응형

vba를 사용하여 엑셀에서 데이터를 잠그는 방법

저는 다른 사람들이 VBA를 사용하여 제 엑셀 시트의 셀 내용을 편집하는 것을 막고 싶습니다.이것을 할 수 있습니까?

먼저 보호하지 않을 셀(사용자 편집 가능)의 잠금 상태를 거짓으로 설정하여 셀을 선택할 수 있습니다.

Worksheets("Sheet1").Range("B2:C3").Locked = False

그러면 시트를 보호할 수 있고 다른 모든 셀이 보호됩니다.VBA 코드가 셀을 수정할 수 있도록 허용하는 코드는 다음과 같습니다.

Worksheets("Sheet1").Protect UserInterfaceOnly:=True

또는

Call Worksheets("Sheet1").Protect(UserInterfaceOnly:=True)

사용해 보십시오.Worksheet.Protect방법, 다음과 같은 것:

Sub ProtectActiveSheet()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Protect DrawingObjects:=True, Contents:=True, _
        Scenarios:=True, Password="SamplePassword"
End Sub

그러나 VBA 코드에 암호를 포함하는 것에 대해 주의해야 합니다.수식을 삭제하는 등의 작은 실수를 방지하는 단순한 장벽만 세우려는 경우에는 반드시 암호가 필요하지 않습니다.

또한 Excel의 VBA에서 특정 작업을 수행하는 방법을 보려면 매크로를 기록하고 매크로가 생성하는 코드를 살펴 보십시오.그것은 VBA를 시작하는 좋은 방법입니다.

예를 들어, 범위 A1에서 I50 사이의 셀을 잠그려면 아래의 코드를 사용합니다.

Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"

다른 경우에는 이미 보호 시트가 있는 경우 아래 코드를 따릅니다.

ActiveSheet.Unprotect Password:="Enter your Password"
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"

워크시트의 변경 이벤트에 캡처된 워크시트 수준에서도 작업을 수행할 수 있습니다.그것이 당신의 요구에 더 적합하다면요.값, 기준 등을 기준으로 동적 잠금을 허용합니다.

Private Sub Worksheet_Change(ByVal Target As Range)

    'set your criteria here
    If Target.Column = 1 Then

        'must disable events if you change the sheet as it will
        'continually trigger the change event
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True

        MsgBox "You cannot do that!"
    End If
End Sub
Sub LockCells()

Range("A1:A1").Select

Selection.Locked = True

Selection.FormulaHidden = False

ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

End Sub

언급URL : https://stackoverflow.com/questions/3037400/how-to-lock-the-data-in-a-cell-in-excel-using-vba

반응형