|
Sub TEST5()
Dim arr, i&, dic As Object, iPosRow&
Dim strFileName$, strPath$, shp As Shape
Dim strFirstAddress$, Rng As Range, rngFind As Range
Set dic = CreateObject("Scripting.Dictionary")
If [B1] = "" Then MsgBox "查询数据为空,请检查!", vbExclamation: Exit Sub
arr = Sheets(2).[A1].CurrentRegion
For i = 2 To UBound(arr)
If Not dic.exists(arr(i, 4)) Then
dic(arr(i, 4)) = Array(CDate(arr(i, 2)), i) '如字典中不存查询数据,则i对数据进行行定位,你的日期为文本,不能进行比对,故转成日期格式
Else
If CDate(arr(i, 2)) > dic(arr(i, 4))(0) Then '如字典已存在,则对日期进行判断,如果日期大于则对字典中日期键值则重新赋值,并重新定位列位置
dic(arr(i, 4)) = Array(CDate(arr(i, 2)), i)
End If
End If
Next i
If Not dic.exists([B1].Value) Then MsgBox "找不到站点,请新增!", vbExclamation: Exit Sub '如果不存在键值则说明查询数据无,进行提示并退出。
Application.ScreenUpdating = False
iPosRow& = dic([B1].Value)(1) '将存在的站定,在arr中定位列号并赋值给iposrow
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & [B1].Value '提前定义好需要保存的文件名
ThisWorkbook.Sheets(1).Copy '将当前第1个工作表拷贝成新工作簿
Set Rng = [A3:F100] '设置查询范围,请自行调整
For i = 1 To UBound(arr, 2) '对第一行数据进行循环查询
Set rngFind = Rng.Find(arr(1, i), , , xlWhole) '全部相同方式进行匹配
If Not rngFind Is Nothing Then
strFirstAddress = rngFind.Address
Do
rngFind.Offset(, 1) = arr(iPosRow, i) '循环方式查找,直到没有为止
Set rngFind = Rng.FindNext(rngFind)
Loop Until rngFind.Address = strFirstAddress
End If
Next i
For Each shp In ActiveSheet.Shapes '将工作簿按钮删除
shp.Delete
Next
ActiveWorkbook.SaveAs strFileName '以机房名称将当前工作簿保存
ActiveWorkbook.Close '关闭工作簿
Set dic = Nothing
Application.ScreenUpdating = True
MsgBox "文件生成完成!", 64
End Sub
|
|