Venerdì, 18 marzo 2022
  3 Risposte
  9.7K visite
0
voti
Disfare
Sto creando un foglio di calcolo per i dati di tendenza per i test analitici di prodotti chimici. Vorrei che ogni riga di dati fosse bloccata una volta che la trascrizione di detti dati è stata verificata dal revisore. Sono in grado di bloccare una singola riga usando questo codice in VBA:

Private Sub Worksheet_Change (ByVal Target As Range)
Se Range("X3") = "No" Allora
Intervallo ("B3: W3"). Bloccato = Falso
ElseIf Range("X3") = "Sì" Allora
Intervallo ("B3: W3"). Bloccato = Vero
End If
End Sub

La colonna X contiene un elenco a discesa con "Sì" e "No" come le due opzioni. Vorrei che ogni riga di dati, man mano che viene aggiunta al foglio, venga bloccata una volta che il revisore ha selezionato sì in questa colonna per assicurarsi che non vengano apportate modifiche indesiderate ai dati passati. È possibile senza dover ripetere il codice about per ogni riga all'infinito?
anni fa, 2
·
#2529
Risposta accettata
1
voti
Disfare
Ciao Stefania,

Per favore prova il codice qui sotto, se hai altre domande, non esitare a chiedermelo.

Amanda

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPassword As String
Dim xRgAddress As String
Dim xLockRgAddress As String
Dim Row As Integer

xPassword = "123456" 'Please replace 123456 with the password that protects the spreadsheet.
On Error Resume Next

If (Target.Column <> 24) Then
Exit Sub
End If

Row = Target.Row


If Target = "Yes" Then
If ActiveSheet.Range("B" & Row & ":W" & Row).Locked = False Then
ActiveSheet.Unprotect (xPassword)
ActiveSheet.Range("B" & Row & ":W" & Row).Locked = True
ActiveSheet.Protect Password:=xPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
ElseIf Target = "No" Then
If ActiveSheet.Range("B" & Row & ":W" & Row).Locked = True Then
ActiveSheet.Unprotect (xPassword)
ActiveSheet.Range("B" & Row & ":W" & Row).Locked = False
ActiveSheet.Protect Password:=xPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End If


End Sub
anni fa, 2
·
#2522
0
voti
Disfare
Ho anche bisogno che sia in grado di modificare lo stato di queste celle da sbloccate a bloccate mentre il foglio di calcolo è protetto, altrimenti questa funzione è inutile.
anni fa, 2
·
#2529
Risposta accettata
1
voti
Disfare
Ciao Stefania,

Per favore prova il codice qui sotto, se hai altre domande, non esitare a chiedermelo.

Amanda

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPassword As String
Dim xRgAddress As String
Dim xLockRgAddress As String
Dim Row As Integer

xPassword = "123456" 'Please replace 123456 with the password that protects the spreadsheet.
On Error Resume Next

If (Target.Column <> 24) Then
Exit Sub
End If

Row = Target.Row


If Target = "Yes" Then
If ActiveSheet.Range("B" & Row & ":W" & Row).Locked = False Then
ActiveSheet.Unprotect (xPassword)
ActiveSheet.Range("B" & Row & ":W" & Row).Locked = True
ActiveSheet.Protect Password:=xPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
ElseIf Target = "No" Then
If ActiveSheet.Range("B" & Row & ":W" & Row).Locked = True Then
ActiveSheet.Unprotect (xPassword)
ActiveSheet.Range("B" & Row & ":W" & Row).Locked = False
ActiveSheet.Protect Password:=xPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End If


End Sub
anni fa, 2
·
#2531
0
voti
Disfare
Grazie mille! Quel codice ha funzionato perfettamente. Sono ancora molto nuovo in VBA, quindi apprezzo molto il tuo aiuto! :)
  • Pagina :
  • 1
Non ci sono ancora risposte per questo post.