Excel - Fórmula de validación de datos

Problema

Tengo una hoja de cálculo que rastrea la asistencia. Lo que quiero hacer es alertar al usuario cuando 3 O MÁS CÉLULAS EN SECUENCIA CONTIENEN LOS MISMOS DATOS . p.ej. Si alguien llama al enfermo, una S se coloca en la celda, si esta persona está enferma Lunes, martes, miércoles, me gustaría alertar al usuario. (3 días en secuencia con los mismos datos). Si la persona está enferma en Thur también, quiero alertar al usuario nuevamente.

Solución

  • 1. Presione ALT + F11 para abrir VBE
  • 2. Presione CTRL + R para abrir el Explorador de proyectos
  • 3. Haga doble clic en la hoja en la que desea este cuadro de mensaje
  • 4. Pegue el código

 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) And (Target.Rows.Count = 1)) Then If Target = "" then Salir Sub End If vPos = "" Application.EnableEvents = False para cada celda en Target If UCase (Cell) "S" Then GoTo Next_Cell vPos = "" iCol = Cell.Column If iCol> = 3 Then If ((Cell = Cell .Offset (0, -2)) And (Cell.Offset (0, -1) = Cell)) Then vPos = -1 End If End If If ((vPos = "") And (iCol> = 2) And ( iCol <Columns.Count)) Then If ((Cell = Cell.Offset (0, -1)) And (Cell.Offset (0, 1) = Cell)) Entonces vPos = 0 End If End If If ((vPos = "") And (iCol <Columns.Count - 1)) Then If ((Cell = Cell.Offset (0, 1)) And (Cell.Offset (0, 2) = Cell)) Entonces vPos = 1 End If End If If (vPos "") Entonces GoTo End_Sub End If Next_Cell: Next End_Sub: Application.EnableEvents = True If (vPos "") Entonces MsgBox "Tres en una fila" End If End Sub 

Si las alertas solo están habilitadas para los días laborables (de lunes a viernes).

 Private Sub Worksheet_Change (ByVal Target As Range) Dim vPos As Variant Dim iCol As Integer Dim CellValue As Variant Dim iOffsetL2 Como Integer Dim iOffsetL1 Como Integer Dim iOffsetR1 Como Integral Dim iOffset2 Como Integer Dim CellL2 Como Variant Dim CellL1 Como Variante Dim Cell02 CellR1 como variante Dim CellR2 como variante If ((Target.Columns.Count = 1) And (Target.Rows.Count = 1)) Entonces If Target = "" Entonces Salir Sub End If vPos = "" 'Salir Sub On Error GoTo End_Sub Application.EnableEvents = False para cada celda en Target Cell0 = UCase (Cell.Value) 'Si Cell0 "S" entonces va a Next_Cell vPos = "" iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 0 iCol = Cell.Column If (IsDate (Celdas (1, iCol))) Entonces CellL2 = "Valor de la basura" CellL1 = "Valor de la basura" CellR1 = "Valor de la basura" CellR2 = "Casilla de selección del" Valor de la basura "(Weekday (Cells (1, iCol), vbMonday) ) Caso es = 1 iOffsetL2 = -2 iOffsetL1 = -2 iOffsetR1 = 0 iOffsetR2 = 0 Caso es = 2 iOffsetL2 = -2 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 0 Caso es = 4 iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 0 iOffsetR2 = 2 Case Is = 5 iOffsetL2 = 0 iOffsetL1 = 0 iOffsetR1 = 2 iOffsetR2 = 2 End Select End If On Error Resume Next CellL2 = Cell.Offset (0, 2 ++) )). Valor CellL1 = Cell.Offset (0, (-1 + iOffsetL1)). Value CellR1 = Cell.Offset (0, (1 + iOffsetR1)). Value CellR2 = Cell.Offset (0, (2 + iOffsetR2) ) .Valor en error GoTo End_Sub CellL2 = UCase (CellL2) CellL1 = UCase (CellL1) CellR1 = UCase (CellR1) CellR2 = UCase (CellR2) Si (iCol + iOffsetL2> 2) Entonces '? ? X If ((CellL2 = Cell0) And (CellL1 = Cell0)) vPos = -1 GoTo End_Sub End If End If If ((iCol + iOffsetL1> 0) And ((iCol - iOffsetR1) <Columns.Count)) Luego ' ? X ? If ((CellL1 = Cell0) And (Cell0 = CellR1)) vPos = 0 GoTo End_Sub End If End If If (iCol <Columns.Count - 1) Entonces 'X? ? If ((Cell0 = CellR1) And (Cell0 = CellR2)) Then vPos = 1 GoTo End_Sub End If End If Next_Cell: Next End_Sub: Application.EnableEvents = True If (vPos "") Entonces MsgBox "Three in a row" End Sub 

Gracias a rizvisa1 por este consejo.

Artículo Anterior Artículo Siguiente

Los Mejores Consejos