|
代码如下。。。
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets(1)
arr = sht.UsedRange
crr = sht.UsedRange.Rows(1)
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = sht.Parent.Path & "\"
.AllowMultiSelect = False
.Title = "选择查询表所在的文件夹:"
If .Show Then p = .SelectedItems(1) Else Exit Sub
End With
p = p & "\"
Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary")
Set dic = CreateObject("scripting.dictionary")
With Workbooks.Open(p & "查询表.xlsx").Sheets(1)
brr = .UsedRange
For j = 1 To UBound(brr, 2)
n = Application.Match(brr(1, j), crr, 0)
If IsNumeric(n) Then dic(n) = ""
Next
Key = dic.keys
n = Key(0)
For i = 2 To UBound(brr)
d(brr(i, 1)) = ""
Next
For k = 2 To UBound(arr)
If Not d.exists(arr(k, n)) Then
r = .Cells(Rows.Count, 1).End(3).Row + 1
For j = 0 To UBound(Key)
.Cells(r, j + 1) = arr(k, Key(j))
Next
End If
Next
d.RemoveAll
' Next
.Parent.Close 1
End With
Set d = Nothing
Set dic = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|