|
- Sub test()
- Dim Conn As Object, rs As Object, Sht As Worksheet
- Dim ar() As String, i As Long, r As Long
- Set Conn = CreateObject("ADODB.Connection")
- If Application.Version < 12 Then
- Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
- Else
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
- End If
- For Each Sht In Worksheets
- With Sht
- If .Name <> "汇总" Then
- If Len(.Range("A1").Value) Then
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- If r > 4 Then
- i = i + 1
- ReDim Preserve ar(1 To i)
- ar(i) = "SELECT '" & .Name & "' AS 工作表名,商品名称,申购数量,单位,单价,采购数量,实收,合计,备注 FROM [" & .Name & "$A4:I" & r & "] WHERE LEN(TRIM(商品名称))"
- End If
- End If
- End If
- End With
- Next
- Set rs = Conn.Execute(Join(ar, " UNION ALL "))
- With Worksheets("汇总")
- .Range("A1").CurrentRegion.Offset(1).ClearContents
- For i = 0 To rs.Fields.Count - 1
- .Range("A1").Offset(0, i) = rs.Fields(i).Name
- Next
- .Range("A2").CopyFromRecordset rs
- End With
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|