20170921xlVBA_SQL蒸发循环查询2
'ARRAY("1991","1992","1993","1994","1996","1997","1998","1999","2001")
Sub ADO_SQL_QUERY_ONE_RNG()
'应用程序设置
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
'错误处理
On Error GoTo ErrHandler
'计时器
Dim StartTime, UsedTime As Variant
StartTime = VBA.Timer
'变量声明
Dim Wb As Workbook
Dim Sht As Worksheet
Dim DataSht As Worksheet
Dim Rng As Range
Dim Arr As Variant
Dim EndRow As Long
Dim DataPath As String
Dim SQL As String
'实例化对象
Set Wb = Application.ThisWorkbook
DataPath = Wb.Path & "\" & "蒸发214.xlsx" 'Wb.FullName
'Set DataSht = Wb.Worksheets("2001")
'Set Sht = Wb.Worksheets("result")
'********************************************************************************************************************
'对象变量声明
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=YES;IMEX=2';Data Source="
'Case Is >= 12
DATA_ENGINE = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;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
'********************************************************************************************************************
'dataname = Array("1991", "1992", "1993", "1994", "1996", "1997", "1998", "1999", "2001")
dataname = Array("2002", "2003", "2004", "2006", "2007", "2008", "2009", "2011", "2012", "2013", "2014")
For i = LBound(dataname) To UBound(dataname)
On Error Resume Next
Wb.Worksheets(dataname(i) & "日子").Delete
On Error GoTo 0
Set Sht = Wb.Worksheets.Add(after:=Wb.Worksheets(Wb.Worksheets.Count))
Sht.Name = dataname(i) & "日子"
With Sht
EndRow = .Cells(.Cells.Rows.Count, 2).End(xlUp).Row
.Cells.ClearContents
.Range("A1:F1").Value = Array("年", "月", "日", "数据", "数据除10", "日期序号")
Set Rng = .Range("A2")
'设置查询语句
SQL = "SELECT 年,月,日,SUM(值),SUM(值)/10,NULL FROM [" & dataname(i) & "$A1:G] WHERE 站点 IS NOT NULL GROUP BY 年,月,日"
'执行查询 返回记录集
'RS.Open SQL, CNN, 1, 1
Set RS = CNN.Execute(SQL)
'复制记录集到指定Range
Rng.CopyFromRecordset RS
End With
Next i
'关闭记录集
RS.Close
'关闭连接器
CNN.Close
'运行耗时
UsedTime = VBA.Timer - StartTime
ErrorExit: '错误处理结束,开始环境清理
Set Wb = Nothing
Set Sht = Nothing
Set Rng = Nothing
'释放对象
Set RS = Nothing
Set CNN = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "错误提示!"
'Debug.Print Err.Description
Err.Clear
'Resume ErrorExit
End If
End Sub
Sub GetDateIndex()
For Each Sht In ThisWorkbook.Worksheets
If Sht.Name Like "*日子" Then
lastYear = CLng(Left(Sht.Name, 4)) - 1
startdate = CDate(lastYear & "/12/31")
Debug.Print startdate
With Sht
EndRow = .Range("A65536").End(xlUp).Row
For i = 2 To EndRow
today = CDate(.Cells(i, 1).Value & "/" & .Cells(i, 2).Value & "/" & .Cells(i, 3).Value)
.Cells(i, 6).Value = DateDiff("d", startdate, today)
Next i
End With
End If
Next Sht
End Sub
20170921xlVBA_SQL蒸发循环查询2的更多相关文章
- Oracle 如何循环查询结果集,进行新增或修改
Oracle的PL/SQL中怎样循环查询的结果集,然后根据查询结果进行判断,是新增或修改操作 loop循环例子 for item in (select a,b,c from table_a where ...
- 2017-09-21xlVBA_蒸发SQL循环查询1
'ARRAY("1991","1992","1993","1994","1996","19 ...
- mysql循环查询树状数据
完整function )) ) CHARSET utf8 BEGIN ) ; ) ; SET str = ''; SET cid =cast(rootId as CHAR); WHILE cid is ...
- sql遍历查询结果sql循环查询结果集sql循环查询
--查询表B,把查询到的数据插入临时表#A中,根据表B 的ID 进行排序:表#A中 的 i 字段 由1开始增加排序: SELECT ROW_NUMBER() OVER ( ORDER ...
- Oracle循环查询结果集 自定义函数
create or replace function Fun_GetRoleIDList(d_fid char) return varchar is rolelist varchar(2000);b ...
- [moka同学笔记]Yii2.0循环查询并对结果累加求和
在控制器中查询好数据 $model 在视图中输入 <?php foreach($model as $key=>$r):?> <tr class="text-cent ...
- oracle中用while循环查询1到100的质数(素数)
declare i number:=1; --表示当前数字 j number:=0; --从2开始,存储判断的数字 sum1 number:=0;--总数begin while(i<100) ...
- sql循环查询树形结构
pid:父类别id ' connect by prior pid = id --查询父祖类别 union ' connect by prior id = pid;--查询子孙类别 这样查询出选中id ...
- SqlServer中循环查询结果集
); begin ; open c_test_main;--打开游标 --开始循环 begin fetch next from c_test_main into @id,@value; --赋值到变量 ...
随机推荐
- MongDB 数据库使用技巧及注意事项
说明 本文记录MongoDB数据库的使用技巧总结,特性发现. 利用TTL特性实现数据定时删除 限制: 你不能创建TTL索引,如果要索引的字段已经在其他索引中使用. 索引不能包含多个字段. 索引的字段必 ...
- selinux 设置的彻底理解 并要 熟练经常的使用
只需要参考这篇文章就好了: http://www.jishux.com/plus/view-631994-1.html 注意 在linux中 两个术语 的严格区分和使用: 改变: change; 改变 ...
- 翻硬币|2013年蓝桥杯B组题解析第八题-fishers
翻硬币 小明正在玩一个"翻硬币"的游戏. 桌上放着排成一排的若干硬币.我们用 * 表示正面,用 o 表示反面(是小写字母,不是零). 比如,可能情形是:oooooo 如果同时翻转左 ...
- java 反射工具
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</ ...
- (转)Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspectives
Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspecti ...
- HDU 5844 LCM Walk(数学逆推)
http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...
- 正则表达式 & re
本节主要内容: 1.正则表达式 2.re模块的使用 一.正则表达式 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE), 计算机 ...
- git core.autocrlf配置 解决Windows和Linux(Mac)换行问题
格式化 格式化是许多开发人员在协作时,特别是在跨平台情况下,遇到的令人头疼的细小问题. 由于编辑器的不同或者Windows程序员在跨平台项目中的文件行尾加入了回车换行符, 一些细微的空格变化会不经意地 ...
- 返回Json格式结果
static string ReturnData(int resultCode, string resultMessage = "", string resultData = &q ...
- Easyui使用心得(1)--DateGrid表格
最近一直在用easyui这个控件,有一点心得,在这里和大家分享一下,也是对自己工作的一个小小的总结,希望可以形成一个完整的Easyui的笔记体系,可以方便更多的人 因为自己也是在摸索中前进,难免有遗漏 ...