Sub UpdateClientDetailWGQ()
Dim Wb As Workbook
Dim Sht As Worksheet
Dim Rng As Range
Dim Arr As Variant
Dim Brr As Variant
Dim dData As Object
Dim dRow As Object
Dim Key As String
Dim OneKey Set dData = CreateObject("Scripting.Dictionary")
Set dRow = CreateObject("Scripting.Dictionary")
Set Wb = Application.ThisWorkbook 'Set Sht = Wb.Worksheets("CPU") '选择文件
Dim FilePath As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path
.Title = "请选择单个Excel工作簿"
.Filters.Clear
.Filters.Add "Excel工作簿", "*.xls*"
If .Show = -1 Then
FilePath = .SelectedItems(1)
Else
MsgBox "您没有选中任何文件夹,本次汇总中断!"
Exit Sub
End If
End With
'查询更新内容
For Each Sht In Wb.Worksheets SQL = "SELECT F2,F9,F10,F11,F12,F13,F14,F15 FROM [" & Sht.Name & "$A2:O] WHERE F9 IS NOT NULL"
Debug.Print SQL
If RecordExistsRunSQL(FilePath, SQL) Then Arr = RunSQLReturnArray(FilePath, SQL)
For j = LBound(Arr, 2) To UBound(Arr, 2)
Key = CStr(Arr(0, j))
'For i = LBound(Arr) To UBound(Arr)
'Debug.Print Key
dData(Key) = Array(Arr(1, j), Arr(2, j), Arr(3, j), Arr(4, j), Arr(5, j), Arr(6, j), Arr(7, j))
'Next i
Next j With Sht
endrow = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row
Set Rng = .Range("A2:O" & endrow)
Brr = Rng.Value
For i = LBound(Brr) To UBound(Brr)
Key = CStr(Brr(i, 2))
'Debug.Print Key
dRow(Key) = i
Next i For Each OneKey In dData.keys
If dRow.exists(OneKey) Then
ar = dData(OneKey)
For j = LBound(ar) To UBound(ar)
Brr(dRow(OneKey), j + 9) = ar(j)
Next j
End If
Next OneKey
Rng.Value = Brr
End With
End If
Next Sht Set Wb = Nothing
Set dData = Nothing
Set dRow = Nothing
Set Sht = Nothing
Set Rng = Nothing End Sub
Public Function RunSQLReturnArray(ByVal DataPath As String, ByVal SQL As String) As Variant()
'对传入数据源地址进行判断
If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then
MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio"
Exit Function
End If
'对传入SQL语句进行判断
If Len(SQL) = 0 Then _
MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio": Exit Function
'对象变量声明
Dim CNN As Object
Dim RS As Object
'数据库引擎——Excel作为数据源
Dim DATA_ENGINE As String
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
DATA_ENGINE = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties='Excel 8.0;HDR=no;IMEX=2';Data Source="
Case Is >= 12
DATA_ENGINE = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=no;IMEX=2'; Data Source= "
End Select '数据库引擎——Excel作为数据源
'Const DATA_ENGINE As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Extended Properties='Excel 12.0;HDR=YES;IMEX=2'; Data Source= "
'创建ADO Connection 连接器 实例
Set CNN = CreateObject("ADODB.Connection")
'On Error Resume Next
'创建 ADO RecordSet 记录集 实例
'Set RS = CreateObject("ADODB.RecordSet")
'连接数据源
CNN.Open DATA_ENGINE & DataPath
'执行查询 返回记录集
' RS.Open SQL, CNN, 1, 1
Set RS = CNN.Execute(SQL)
RunSQLReturnArray = RS.GetRows()
'关闭记录集
'RS.Close
'关闭连接器
CNN.Close
'释放对象
Set RS = Nothing
Set CNN = Nothing
End Function Public Function RecordExistsRunSQL(ByVal DataPath As String, ByVal SQL As String) As Boolean
'对传入数据源地址进行判断
If Len(DataPath) = 0 Or Len(Dir(DataPath)) = 0 Then
RecordExistsRunSQL = False
MsgBox "数据源地址为空或者数据源文件不存在!", vbInformation, "NS Excel Studio"
Exit Function
End If
'对传入SQL语句进行判断
If Len(SQL) = 0 Then
RecordExistsRunSQL = False
MsgBox "SQL语句不能为空!", vbInformation, "NS Excel Studio"
Exit Function
End If
'对象变量声明
Dim CNN As Object
Dim RS As Object
'数据库引擎——Excel作为数据源
Dim DATA_ENGINE As String
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
DATA_ENGINE = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties='Excel 8.0;HDR=no;IMEX=2';Data Source="
Case Is >= 12
DATA_ENGINE = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=no;IMEX=2'; Data Source= "
End Select
'数据库引擎——Excel作为数据源
'Const DATA_ENGINE As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Extended Properties='Excel 12.0;HDR=YES;IMEX=2'; Data Source= " '创建ADO Connection 连接器 实例
Set CNN = CreateObject("ADODB.Connection")
On Error Resume Next
'创建 ADO RecordSet 记录集 实例
Set RS = CreateObject("ADODB.RecordSet")
'连接数据源
CNN.Open DATA_ENGINE & DataPath
'执行查询 返回记录集
RS.Open SQL, CNN, 1, 1
'返回函数结果
If RS.RecordCount > 0 Then
RecordExistsRunSQL = True
Else
RecordExistsRunSQL = False
End If
'关闭记录集
RS.Close
'关闭连接器
CNN.Close
'释放对象
Set RS = Nothing
Set CNN = Nothing
End Function

  

20170923xlVBA_UpdateClientDetailSQL_Dictionary的更多相关文章

随机推荐

  1. CSS的再深入2(更新中···)

    在上一章中,我们又引出了一个知识点: margin的问题 margin:0 auto:(上下为0,左右自适应)会解决元素的居中问题(auto 自适应)前提是给这个元素设置width 同时,我们又要学习 ...

  2. Vue学习【第一篇】:Vue初识与指令

    什么是Vue 什么是Vue Vue.js是一个渐进式JavaScript框架它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 渐进式:vue从小到控制页面中的一个变量 ...

  3. 洛谷1968美元汇率 dp

    P1968 美元汇率 dp 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 输入格式: ...

  4. POJ 1191 棋盘分割(区间DP)题解

    题意:中文题面 思路:不知道直接暴力枚举所有情况行不行... 我们可以把答案转化为 所以答案就是求xi2的最小值,那么我们可以直接用区间DP来写.设dp[x1][y1][x2][y2][k]为x1 y ...

  5. Calculate difference between consecutive data points in a column from a file

    cat > temp0015101269125  awk 'p{print $0-p}{p=$0}' temp00152-633-7 REF: https://www.unix.com/shel ...

  6. P3980 [NOI2008]志愿者招募

    思路 巧妙的建图 因为每个志愿者有工作的时段,所以考虑让一个志愿者的流量能够从S流到T产生贡献 所以每个i向i+1连INF-a[x]的边(类似于k可重区间集),每个si向ti连边cap=INF,cos ...

  7. 挺不错的Java自学网站

    挺不错的Java自学网站 http://how2j.cn?p=29369

  8. [jsp & thymeleaf] - jsp和thymeleaf的共存解析

    做项目时因为有些老jsp还需要测试用到,所以之前的thymeleaf也需要保持,配置如下: https://github.com/deadzq/jsp-thymeleaf 等空余时间在做详解吧!

  9. Images之Dockerfile中的命令2

    COPY COPY has two forms: COPY [--chown=<user>:<group>] <src>... <dest> COPY ...

  10. WinForm 中 comboBox控件数据绑定

    一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<string>(); list.Add("11 ...