VBA Code: Block specified cells without protecting the entire spreadsheet

Asked

Viewed 2,198 times

0

Good afternoon

You can help me with the formula below?

I need to include more columns and other specific cells.. but I don’t want to block everything.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then
    If Target.Row = 3 Or Target.Row = 5 Then
        Beep
        Cells(Target.Row, Target.Column).Offset(0, 1).Select
    End If
End If
End Sub

1 answer

1


Follow the code with improvements:

The rules are simple, in the code where this: vStrLocalsProibidos = "A3,A5" Voce must declare all places where Voce wishes not to have permission. Interspersed with comma (,) Example.

I want to block Columns A, C, D, E, G up to Z and also block rows 3 and 5 and cell B2. Thus Voce has a lot of flexibility to make any combination.

vStrLocalsProibidos = "A:A,C:D,E:E,G:Z,3:3,5:5,B2"

suggestion. not to have to type this down. Select all areas you want to block with the mouse and CTRL pressed. And then immediately you use

?Selection.address

to take the addresses without suffering. D

Dim vErrLineMove As Long

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim vStrLocaisProibidos As String
Dim vRngReturn As Variant
Dim vMaxError As Long

    'definido para parar depois que a macro tenta colocar um novo local
    'depois de 10 tentativas. Ele para e mantem o cursor a onde for.
    vMaxError = 10
    vStrLocaisProibidos = "A:A,C:D,E:E,G:Z,3:3,5:5,B2"

    If vStrLocaisProibidos = vbNullString Then Exit Sub
    Set vRngReturn = Application.Intersect(Target.Worksheet.Range(vStrLocaisProibidos), Target)

    If Not vRngReturn Is Nothing Then
        Beep
        If vErrLineMove < vMaxError Then
            vErrLineMove = vErrLineMove + 1
            Cells(Target.Row, Target.Column).Offset(0, 1).Select
        End If
    End If

    vErrLineMove = 0

End Sub

att.

Hudson Komuro

  • Worked perfectly!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.