Excel - Formula di convalida dei dati

Problema

Ho un foglio di calcolo che tiene traccia delle presenze. Quello che voglio fare è avvisare l'utente ogni volta che 3 O PIÙ CELLULE IN SEQUENZA CONTENGONO GLI STESSI DATI . per esempio. Se qualcuno chiama malato, una S viene messa nella cella, se questa persona è malata, lunedì, mercoledì, mercoledì, vorrei avvisare l'utente. (3 giorni in sequenza con gli stessi dati). Se la persona è malata anche su Thur, voglio avvisare l'utente di nuovo.

Soluzione

  • 1. Premere ALT + F11 per aprire VBE
  • 2. Premere CTRL + R per aprire Gestione progetti
  • 3. Fare doppio clic sul foglio in cui si desidera questa finestra di messaggio
  • 4. Incolla il codice

 Private Sub Worksheet_Change (ByVal Target As Range) Dim vPos As Variant Dim iCol As Integer Dim CellValue As Variant If ((Target.Columns.Count = 1) E (Target.Rows.Count = 1)) Then If Target = "" Quindi Exit Sub End If vPos = "" Application.EnableEvents = False per ogni cella nella destinazione Se UCase (cella) "S" Quindi GoTo Next_Cell vPos = "" iCol = Cell.Column If iCol> = 3 Then If ((Cell = Cell .Offset (0, -2)) E (Cell.Offset (0, -1) = Cella)) Quindi vPos = -1 End If End If If ((vPos = "") And (iCol> = 2) E ( iCol <Columns.Count)) Then If ((Cell = Cell.Offset (0, -1)) E (Cell.Offset (0, 1) = Cell)) Quindi vPos = 0 End If End If If ((vPos = "") E (iCol <Columns.Count - 1)) Then If ((Cell = Cell.Offset (0, 1)) E (Cell.Offset (0, 2) = Cell)) Quindi vPos = 1 End If End If If (vPos "") Then GoTo End_Sub End If Next_Cell: Next End_Sub: Application.EnableEvents = True If (vPos "") Then MsgBox "Three in a row" End If End Sub 

Se gli avvisi devono essere abilitati solo per i giorni feriali (dal lunedì al venerdì).

 Private Sub Worksheet_Change (ByVal Target As Range) Dim vPos As Variant Dim iCol As Integer Dim CellValue As Variant Dim iOffsetL2 As Integer Dim iOffsetL1 As Integer Dim iOffsetR1 As Integer Dim iOffset2 As Integer Dim CellL2 As Variant Dim CellL1 As Varian Dim Cell0 Come Dim Variant CellR1 As Variant Dim CellR2 As Variant If ((Target.Columns.Count = 1) And (Target.Rows.Count = 1)) Then If Target = "" Esci da sub-end If vPos = "" 'Esci da sub-errore GoTo End_Sub Application.EnableEvents = False Per ogni cella In Target Cell0 = UCase (Cell.Value) 'Se Cell0 "S" Allora GoTo Next_Cell vPos = "" iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 0 iCol = Cell.Column If (IsDate (Cells (1, iCol))) CellL2 = "Garbage Value" CellL1 = "Garbage Value" CellR1 = "Garbage Value" CellR2 = "Garbage Value" Seleziona caso (Weekday (Cells (1, iCol), vbMonday) ) Case Is = 1 iOffsetL2 = -2 iOffsetL1 = -2 iOffsetR1 = 0 iOffsetR2 = 0 Caso È = 2 iOffsetL2 = -2 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 0 Caso È = 4 iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 2 Caso È = 5 iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 2 iOffsetR2 = 2 Fine Seleziona Fine Se On Error Riprendi Avanti CellL2 = Cell.Offset (0, (-2 + iOffsetL2 )). Valore CellL1 = Cell.Offset (0, (-1 + iOffsetL1)). Valore CellR1 = Cell.Offset (0, (1 + iOffsetR1)). Valore CellR2 = Cell.Offset (0, (2 + iOffsetR2) ) .Valore On Error GoTo End_Sub CellL2 = UCase (CellL2) CellL1 = UCase (CellL1) CellR1 = UCase (CellR1) CellR2 = UCase (CellR2) Se (iCol + iOffsetL2> 2) Quindi '? ? X If ((CellL2 = Cell0) E (CellL1 = Cell0)) Quindi vPos = -1 GoTo End_Sub End If End If If ((iCol + iOffsetL1> 0) E ((iCol - iOffsetR1) <Columns.Count)) Quindi ' ? X ? Se ((CellL1 = Cell0) E (Cell0 = CellR1)) Quindi vPos = 0 GoTo End_Sub End If End If If (iCol <Columns.Count - 1) Quindi 'X? ? Se ((Cell0 = CellR1) E (Cell0 = CellR2)) Quindi vPos = 1 GoTo End_Sub End If End If Next_Cell: Next End_Sub: Application.EnableEvents = True If (vPos "") Then MsgBox "Three in a row" End If End Sub 

Grazie a rizvisa1 per questo suggerimento.

Articolo Precedente Articolo Successivo

I Migliori Consigli