|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
用原程序修改
Private Sub workbook_open()
'计算每天一天的最大,最小的LOTNO
Dim arr, brr, crr, d, d2, i&, j&
crr = [m2:p100] '指定范围
ReDim arr(1 To UBound(crr), 1 To 1)
For i = 1 To UBound(crr)
If crr(i, 4) = 1 Then '当P列为1时,继续计算,否则退出
Set d = CreateObject("scripting.dictionary") ' 创建字典,用于日期变量的值
Set d2 = CreateObject("scripting.dictionary") ' 创建字典,用于流水号变量的值
s = s + 1: arr(s, 1) = crr(i, 1)
'For h = 1 To s
x = Split(crr(i, 1), ";") 'F列为1时,对D列的值以";"为界进行分割
For j = 1 To UBound(x) 'j设为1开始,去掉第一个;的值,如果LOTNO串联中,第一个没有";",j应改为0开始
m = Left(x(j), 6): n = Val(Right(x(j), 5)) '分别取出日期及流水号
If Not d.exists(m) Then '写日期变量写入字典
d(m) = n
Else
If n < d(m) Then d(m) = n
End If
If Not d2.exists(m) Then
d2(m) = n '写流水号变量写入字典
Else
If n > d2(m) Then d2(m) = n
End If
Next
ReDim brr(1 To d.Count, 1 To 2)
a = d.keys: b = d.items
For k = 0 To d.Count - 1
brr(k + 1, 1) = a(k) & Format(b(k), "00000")
brr(k + 1, 2) = a(k) & Format(d2(a(k)), "00000")
Next
[it1].Resize(d.Count, 1) = Application.Transpose(a)
[iu1].Resize(UBound(brr), 2) = brr
[iu:iv].Replace "0", "", 1
[it:iv].Sort Key1:=[it1], Order1:=xlAscending, Header:=xlNo
[it1].Resize(d.Count, 1).Copy Cells(i + 1, 2)
[iu1].Resize(UBound(brr), 2).Copy Cells(i + 1, "j")
[it:iv] = ""
End If '如果F列不等于1,退出进行下一循环
Set d = Nothing '清空字典
Set d2 = Nothing
Next '进行下一循环
End Sub |
|