|
楼主 |
发表于 2015-11-13 16:49
|
显示全部楼层
本帖最后由 autumnalRain 于 2015-11-13 17:02 编辑
附上百度不到到谷歌老师指导后的代码
- Dim arr
- Sub TEST()
- Dim CONN As Object, rst As Object
- strconn = "provider=microsoft.ACE.OLEDB.12.0;extended properties='Excel 12.0;hdr=yes';data source=" & ThisWorkbook.FullName '2007及以上
- 'strconn = "provider=microsoft.JET.OLEDB.4.0;extended properties='Excel 8.0;hdr=yes';data source=" & ThisWorkbook.FullName'2003
- Sql = "select * from [采集$] where 标题 not in (select 标题 from [查询$C4:C17])"
- Set CONN = CreateObject("ADODB.CONNECTION")
- Set rst = CreateObject("ADODB.recordset")
- CONN.Open strconn
- rst.Open Sql, CONN, 3, 3
- arr = rst.getrows
- 'Stop
- Sheets("采集").Cells.Value = ""
- For i = 1 To rst.Fields.Count
- Sheets("采集").Cells(1, i) = rst.Fields(i - 1).Name
- Next
- x = UBound(arr, 2) + 1: y = UBound(arr, 1) + 1
- Sheets("采集").Range("a2").Resize(UBound(arr, 2) + 1, UBound(arr, 1) + 1) = Transpose2(arr)
- End Sub
- Public Function Transpose2(arr) 'http://Excel880.com 自制转置函数,系统函数有缺陷,将二维数组行列转置
- Dim brr, L1, U1, L2, U2, i&, j&
- L1 = LBound(arr, 1): U1 = UBound(arr, 1)
- L2 = LBound(arr, 2): U2 = UBound(arr, 2)
- ReDim brr(L2 To U2, L1 To U1)
- For i = L1 To U1
- For j = L2 To U2
- brr(j, i) = arr(i, j)
- Next
- Next
- Transpose2 = brr
- End Function
复制代码
|
|