|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Option Explicit
- Sub Demo()
- Dim i As Long, j As Long
- Dim arrData, rngData As Range
- Dim arrRes, r As Range, bFlag As Boolean
- Dim RowCnt As Long, ColCnt As Long
- Set rngData = ActiveSheet.Range("A1").CurrentRegion
- arrData = rngData.Value
- RowCnt = UBound(arrData)
- ColCnt = UBound(arrData, 2)
- ReDim arrRes(1 To RowCnt, 0)
- For i = LBound(arrData) + 1 To UBound(arrData)
- bFlag = True
- Set r = Cells(i, 2).Resize(1, ColCnt - 2)
- If arrData(i, ColCnt) = 0 Then ' 规则4
- arrRes(i, 0) = arrData(1, ColCnt)
- ElseIf Application.CountIf(r, arrData(i, 1)) = ColCnt - 2 Then ' 规则2
- arrRes(i, 0) = arrData(1, 2)
- Else
- For j = ColCnt - 1 To 2 Step -1 ' 规则3
- If arrData(i, j) = 0 Then
- arrRes(i, 0) = arrData(1, j + 1)
- bFlag = False
- Exit For
- End If
- Next j
- If bFlag Then ' 规则1
- For j = 2 To ColCnt - 1
- If arrData(i, j) < arrData(i, 1) Then
- arrRes(i, 0) = arrData(1, j)
- Exit For
- End If
- Next j
- End If
- End If
- Next i
- Cells(1, ColCnt + 1).Resize(RowCnt, 1).Value = arrRes
- End Sub
复制代码 |
|