|
- Option Explicit
- Private Function GetSelectSQLString()
- Dim filePath$, folderPath$, FileDialog
-
- Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
-
- FileDialog.Title = "请选择数据文件夹"
-
- FileDialog.Show
-
- If FileDialog.SelectedItems.Count = 0 Then
-
- Set FileDialog = Nothing
-
- GetSelectSQLString = ""
-
- Exit Function
- End If
-
- folderPath = FileDialog.SelectedItems(1)
-
- filePath = Dir(folderPath & "\*.xls*")
-
- Dim sqlDic As Object
-
- Set sqlDic = CreateObject("scripting.dictionary")
-
- While filePath <> ""
-
- sqlDic(filePath) = "select 借款人,业务编号,发放金额,发放日期,到期日期,余额,cdbl(预计收回金额) as 预计收回金额 From [excel 12.0;imex=1;database=" & folderPath & "" & filePath & "].[Sheet1$] where 预计收回金额 is not null"
-
- filePath = Dir()
-
- Wend
-
- GetSelectSQLString = Join(sqlDic.items, " union ")
-
- Set sqlDic = Nothing
-
- Set FileDialog = Nothing
- End Function
- Sub demo()
- Dim conn As Object, rs As Object, sqlString$
-
- sqlString = GetSelectSQLString
-
- If InStr(sqlString, "select") < 0 Then Exit Sub
-
- Set conn = CreateObject("adodb.connection")
-
- Set rs = CreateObject("adodb.recordset")
-
- conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='excel 12.0;HDR=YES;imex=2';Data Source=" & ThisWorkbook.FullName
-
- '可以注释掉直接使用返回的SQL语句
- sqlString = "select Summary.借款人,Summary.业务编号,Summary.发放金额,Summary.发放日期,Summary.到期日期,Summary.余额,temp.预计收回金额 From [Sheet1$] as Summary Left Join (" & sqlString & ") as temp on Summary.借款人=temp.借款人 and Summary.业务编号=temp.业务编号 and Summary.发放金额=temp.发放金额 and Summary.发放日期=temp.发放日期 and Summary.到期日期=temp.到期日期 and Summary.余额=temp.余额"
-
- rs.Open sqlString, conn
-
- ThisWorkbook.Worksheets(1).Range("a2").CopyFromRecordset rs
-
- Set rs = Nothing
-
- Set conn = Nothing
-
- End Sub
复制代码 |
|