|
- Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 'ThisWorkbook代码区,除了"打印页"之外的各表"$E$1"变动时自动响应
- If Sh.Name = "打印页" Then Exit Sub
- If Target.Address <> "$E$1" Then Exit Sub
- Dim cnn As Object, SQL$, s$, MyPath$, MyFile$, m&, n&, t$
- Dim Mydate As Date, d As Object, arr, brr(), i&, j&, r
- Application.ScreenUpdating = False
- Set d = CreateObject("scripting.dictionary")
- Mydate = Target.Value
- arr = Range("b3:b" & Range("b" & Rows.Count).End(xlUp).Row)
- ReDim brr(1 To UBound(arr), 2 To 4)
- For i = 1 To UBound(arr)
- d(arr(i, 1)) = i
- Next
- On Error Resume Next
- Set cnn = CreateObject("ADODB.Connection")
- MyPath = ThisWorkbook.Path & "\数据" '路径自己修改
- MyFile = Dir(MyPath & "*.xlsx")
- Do While MyFile <> ""
- n = n + 1
- If n = 1 Then
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyFile
- Else
- t = "[Excel 12.0;Database=" & MyPath & MyFile & "]."
- End If
- m = m + 1
- If m > 49 Then
- arr = cnn.Execute(SQL).getrows
- For i = 0 To UBound(arr, 2)
- r = d(arr(1, i))
- If r <> "" Then
- For j = 2 To 4
- brr(r, j) = arr(j, i)
- Next
- End If
- Next
- m = 1
- SQL = ""
- End If
- If Len(SQL) Then SQL = SQL & " union all "
- SQL = SQL & "select * from " & t & "[个人工作记录表$b2:f] where 日期=#" & Mydate & "#"
- MyFile = Dir()
- Loop
- If Len(SQL) Then
- arr = cnn.Execute(SQL).getrows
- For i = 0 To UBound(arr, 2)
- r = d(arr(1, i))
- If r <> "" Then
- For j = 2 To 4
- brr(r, j) = arr(j, i)
- Next
- End If
- Next
- End If
- ActiveSheet.UsedRange.Offset(2, 2).ClearContents
- [c3].Resize(UBound(brr), 3) = brr
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|