|
楼主 |
发表于 2025-1-15 21:58
|
显示全部楼层
感谢大神的帮助。代码完美实现!
如果后面的差异列也用代码就更完美。
我用你的方法加了一个for 循环 差异可以自动填入了!非常感谢
Sub ykcbf() '//2025.1.15
Application.ScreenUpdating = False
arr = Sheets("数据源").UsedRange
ReDim brr(1 To 1000, 1 To 2)
ReDim crr(1 To 1000, 1 To 2)
ReDim drr(1 To 1000, 1 To 2)
With Sheets("Sheet2")
.[a7].Resize(1000, 4) = Empty
xm1 = .[b3].Value: xm2 = .[d3].Value
rq1 = .[b4].Value: rq2 = .[d4].Value
m1 = .[b5].Value: m2 = .[d5].Value
If m1 <= 0 Or m2 <= 0 Then Exit Sub '避免天数小于等于0
For i = 2 To UBound(arr)
If arr(i, 2) >= rq1 Then
If arr(i, 3) = xm1 Then
m = m + 1
brr(m, 1) = arr(i, 2)
brr(m, 2) = arr(i, 6)
If m = m1 Then Exit For
End If
End If
Next
For i = 2 To UBound(arr)
If arr(i, 2) >= rq2 Then
If arr(i, 3) = xm2 Then
n = n + 1
crr(n, 1) = arr(i, 2)
crr(n, 2) = arr(i, 6)
If n = m2 Then Exit For
End If
End If
Next
For i = 1 To m1
y = y + 1
drr(y, 1) = crr(i, 2) - brr(i, 2)
If y = m1 Then Exit For
Next
.[a7].Resize(m1, 2) = brr
.[c7].Resize(m2, 2) = crr
.[E7].Resize(m1, 1) = drr
End With
Application.ScreenUpdating = True
MsgBox "OK!"
End Sub |
|