Advertisement
Dmaxiya

CheckConsecutiveColumns

Nov 8th, 2024
64
0
23 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 表格内填以下公式:
  2. =SWITCH(B2,"一星",IF(CheckConsecutiveColumns(E2:L2,">=",IF(D2="困难",85,95),6),"升级",IF(CheckConsecutiveColumns(E2:L2,"<",70,3),"优化","正常")),"二星",IF(CheckConsecutiveColumns(E2:L2,">=",IF(D2="困难",85,95),9),"升级",IF(CheckConsecutiveColumns(E2:L2,"<",75,3),"降级","正常")),"三星",IF(CheckConsecutiveColumns(E2:L2,"<",80,3),"降级","正常"),"四星",IF(CheckConsecutiveColumns(E2:L2,"<",IF(D2="困难",70,85),3),"降级","正常"),"五星",IF(CheckConsecutiveColumns(E2:L2,"<",IF(D2="困难",70,85),3),"降级","正常"),"优化","优化","转岗","转岗","——","——")
  3.  
  4. vba 填以下代码:
  5. Option Explicit
  6.  
  7. Function CheckConsecutiveColumns(rng As Range, op As String, num As Double, n As Integer) As Boolean
  8.     Dim i As Integer
  9.     Dim j As Integer
  10.     Dim count As Integer
  11.     Dim totalColumns As Integer
  12.     Dim result As Boolean
  13.    
  14.     If op <> "=" And op <> "<" And op <> ">" And op <> ">=" And op <> "<=" Then
  15.         MsgBox ("op 只允许为 >, <, = 中的一个")
  16.         CheckConsecutiveColumns = rng
  17.     End If
  18.    
  19.     ' 计算总列数
  20.    totalColumns = rng.Columns.count
  21.    
  22.     ' 检查每一列
  23.    For i = 1 To totalColumns - n + 1
  24.         ' 初始化计数器
  25.        count = 0
  26.        
  27.         ' 检查连续的 n 列
  28.        For j = i To totalColumns
  29.            
  30.             If IsError(rng.Cells(1, j).Value) Then
  31.                 count = 0
  32.             ElseIf rng.Cells(1, j).Value = Empty Then
  33.                 count = 0
  34.             Else
  35.                 result = executeOp(rng.Cells(1, j).Value, op, num)
  36.                 If result Then
  37.                     count = count + 1
  38.                 Else
  39.                     count = 0
  40.                 End If
  41.             End If
  42.            
  43.  
  44.             ' 如果找到连续的 n 列,则返回 True
  45.            If count = n Then
  46.                 CheckConsecutiveColumns = True
  47.                 Exit Function
  48.             End If
  49.         Next j
  50.     Next i
  51.    
  52.     ' 如果没有找到连续的 m 列,则返回 False
  53.    CheckConsecutiveColumns = False
  54. End Function
  55.  
  56. Function executeOp(x As String, op As String, num As Double) As Boolean
  57.     If op = "=" Then
  58.         executeOp = (x = num)
  59.     ElseIf op = "<" Then
  60.         executeOp = (x < num)
  61.     ElseIf op = ">" Then
  62.         executeOp = (x > num)
  63.     ElseIf op = ">=" Then
  64.         executeOp = (x >= num)
  65.     ElseIf op = "<=" Then
  66.         executeOp = (x <= num)
  67.     End If
  68. End Function
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement