|
Sub test1() '
Dim Conn As Object, rs As Object, Sht As Worksheet
Dim strConn As String, ar() As String, i As Integer
Worksheets("收入汇总 ").Activate
Cells.ClearContents
Set Conn = CreateObject("ADODB.Connection")
If Application.Version < 12 Then
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
Else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
End If
Conn.Open strConn & ThisWorkbook.FullName
For Each Sht In Worksheets
With Sht
If .Name <> ActiveSheet.Name Then
If .Visible = -1 Then
i = i + 1
ReDim Preserve ar(1 To i)
ar(i) = "SELECT * FROM [" & .Name & "$" & .Range("A1").CurrentRegion.Address(0, 0) & "]"
End If
End If
End With
Next
Set rs = Conn.Execute(Join(ar, " UNION ALL "))
With Range("A1")
For i = 0 To rs.Fields.Count - 1
.Offset(0, i) = rs.Fields(i).Name
Next
.Offset(1).CopyFromRecordset rs
.CurrentRegion.Borders.LineStyle = xlContinuous
End With
Set rs = Nothing
Conn.Close
Set Conn = Nothing
Beep
End Sub |
|