|
本帖最后由 CheryBTL 于 2013-10-18 10:18 编辑
先凑一个了,如果学委能提前给出时间要求最好了:- Sub CheryBTL()
- Dim i As Integer, m As Integer
- Dim n1 As Integer, n2 As Integer, n3 As Integer
- Dim ar, Re() As String
- Dim t As Single
- t = Timer
- ar = Sheets("数据源").Range("A1").CurrentRegion
- ReDim Re(1 To (UBound(ar) / 7) * 3, 1 To 10) As String
- For i = 2 To UBound(ar)
- If i Mod 7 = 2 Then n1 = 0: n2 = 0: n3 = 0:m=(i-2)/7*3+1
- Re(m, 3) = "CAP前"
- Re(m + 1, 3) = "CAP后"
- Re(m + 2, 3) = "完成品"
- If i Mod 7 = 2 Then Re(m, 1) = ar(i, 1): Re(m, 2) = ar(i, 2)
- If ar(i, 5) <> "" Then n1 = n1 + 1: Re(m, 3 + n1) = ar(i, 3)
- If ar(i, 6) <> "" Then n2 = n2 + 1: Re(m + 1, 3 + n2) = ar(i, 3)
- If ar(i, 7) <> "" Then n3 = n3 + 1: Re(m + 2, 3 + n3) = ar(i, 3)
- Next i
- With Sheets("结果")
- .Range("A2").CurrentRegion.ClearContents
- .Range("A2").Resize(UBound(Re), 10) = Re
- End With
- MsgBox Timer - t
- End Sub
复制代码 再改下,把VBA函数MOD和INT完全省略掉,也有出现0的,哈哈:- Sub CheryBTL2()
- Dim i As Integer, m As Integer, j As Integer
- Dim n1 As Integer, n2 As Integer, n3 As Integer
- Dim ar, Re() As String
- Dim t As Single
- t = Timer
- With Sheets("数据源")
- ar = .Range("A1").CurrentRegion
- End With
- ReDim Re(1 To UBound(ar) / 7 * 3, 1 To 10) As String
- j = 2
- m = -2
- For i = 2 To UBound(ar)
- If i = j Then
- j = j + 7
- m = m + 3
- Re(m, 1) = ar(i, 1): Re(m, 2) = ar(i, 2)
- n1 = 3: n2 = 3: n3 = 3
- End If
- Re(m, 3) = "CAP前"
- Re(m + 1, 3) = "CAP后"
- Re(m + 2, 3) = "完成品"
- If ar(i, 5) <> "" Then n1 = n1 + 1: Re(m, n1) = ar(i, 3)
- If ar(i, 6) <> "" Then n2 = n2 + 1: Re(m + 1, n2) = ar(i, 3)
- If ar(i, 7) <> "" Then n3 = n3 + 1: Re(m + 2, n3) = ar(i, 3)
- Next i
- With Sheets("结果")
- .Range("A2").CurrentRegion.ClearContents
- .Range("A2").Resize(UBound(Re), 10) = Re
- End With
- MsgBox Timer - t
- End Sub
复制代码 再次减少循环的次数:- Sub CheryBTL3() '再减少参数
- Dim i As Integer, j As Integer
- Dim n1 As Integer, n2 As Integer, n3 As Integer
- Dim ar, Re() As String
- Dim t As Single
- t = Timer
- With Sheets("数据源")
- ar = .Range("A1").CurrentRegion
- End With
- ReDim Re(1 To UBound(ar) / 7 * 3, 1 To 10) As String
- m = -2
- For i = 2 To UBound(ar) Step 7
- m = m + 3
- Re(m, 1) = ar(i, 1): Re(m, 2) = ar(i, 2)
- n1 = 3: n2 = 3: n3 = 3
- Re(m, 3) = "CAP前"
- Re(m + 1, 3) = "CAP后"
- Re(m + 2, 3) = "完成品"
- For j = 0 To 6
- If ar(i + j, 5) <> "" Then n1 = n1 + 1: Re(m, n1) = ar(i, 3)
- If ar(i + j, 6) <> "" Then n2 = n2 + 1: Re(m + 1, n2) = ar(i, 3)
- If ar(i + j, 7) <> "" Then n3 = n3 + 1: Re(m + 2, n3) = ar(i, 3)
- Next j
- Next i
- With Sheets("结果")
- .Range("A2").CurrentRegion.ClearContents
- .Range("A2").Resize(UBound(Re), 10) = Re
- End With
- MsgBox Timer - t
- End Sub
复制代码 |
|