|
本帖最后由 张雄友 于 2014-10-31 21:30 编辑
SQL实现香川多列组合。- Sub SQL也能实现组合()
- tt = Timer
- Dim Conn As Object, Rst As Object, r&
- Dim strConn As String, strSQL As String
- Dim i As Integer, PathStr As String
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- PathStr = ThisWorkbook.FullName
- Select Case Application.Version * 1
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
- Conn.Open strConn
- '非最大行号全部要忽略空值!
- strSQL = "select * from [Data$A1:A10],[Data$B1:B10],[Data$C1:C10],[Data$D1:D10],[Data$E1:E12] " & _
- "where len(前天) and len(昨天) and len(今天) and len(明天) "
- Set Rst = Conn.Execute(strSQL)
- With Sheets("Result")
- .Cells.Clear
- For i = 0 To Rst.Fields.Count - 1
- .Cells(1, i + 1) = Rst.Fields(i).Name
- Next i
- .Range("A2").CopyFromRecordset Rst
- .Cells.EntireColumn.AutoFit
- r = Range("A1048576").End(xlUp).Row - 1
- End With
- Rst.Close
- Conn.Close
- Set Conn = Nothing
- Set Rst = Nothing
- MsgBox "用时" & Format(Timer - tt, "0.00") & "秒" & Chr(10) & "共生成" & r & "种组合!"
- End Sub
复制代码
|
|