Advertisement
Dmaxiya

CheckConsecutiveColumns

Nov 8th, 2024
14
0
16 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Function CheckConsecutiveColumns(rng As Range, op As String, num As Double, n As Integer) As Boolean
  4. Dim i As Integer
  5. Dim j As Integer
  6. Dim count As Integer
  7. Dim totalColumns As Integer
  8. Dim result As Boolean
  9.  
  10. If op <> "=" And op <> "<" And op <> ">" Then
  11. MsgBox ("op 只允许为 >, <, = 中的一个")
  12. CheckConsecutiveColumns = rng
  13. End If
  14.  
  15. ' 计算总列数
  16. totalColumns = rng.Columns.count
  17.  
  18. ' 检查每一列
  19. For i = 1 To totalColumns - n + 1
  20. ' 初始化计数器
  21. count = 0
  22.  
  23. ' 检查连续的 n 列
  24. For j = i To totalColumns
  25. result = executeOp(rng.Cells(1, j).Value, op, num)
  26. If result Then
  27. count = count + 1
  28. Else
  29. count = 0
  30. End If
  31.  
  32. ' 如果找到连续的 n 列,则返回 True
  33. If count = n Then
  34. CheckConsecutiveColumns = True
  35. Exit Function
  36. End If
  37. Next j
  38. Next i
  39.  
  40. ' 如果没有找到连续的 m 列,则返回 False
  41. CheckConsecutiveColumns = False
  42. End Function
  43.  
  44. Function executeOp(x As Double, op As String, num As Double) As Boolean
  45. If op = "=" Then
  46. executeOp = (x = num)
  47. ElseIf op = "<" Then
  48. executeOp = (x < num)
  49. Else
  50. executeOp = (x > num)
  51. End If
  52. End Function
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement