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; --赋值到变量 ...
随机推荐
- django基础 -- 5. ORM 数据库操作
一. ORM 对象关系映射 类 ------ 表 类对象 ------ 记录 类属性 ------ 字段 二. 连接数据库配置 1.在 setting.py 文件中重新设置 ...
- 1-1.flutter学习笔记(一)git入门(msysgit图文安装)
1.下载git-for-windows (1)常用的GitHub客户端msysgit,也就是git-for-windows. (2)登录官网 https://git-for-windows.githu ...
- Rime输入法的配色方案
致青春 so_young: name: "致青春/So Young" author: "五磅兔 zcunlin@foxmail.com" text_color: ...
- Druid介绍
Druid (大数据实时统计分析数据存储) Druid 是一个为在大数据集之上做实时统计分析而设计的开源数据存储.这个系统集合了一个面向列存储的层,一个分布式.shared-nothing的架构,和一 ...
- Centos7更改网卡名称Eth0并配置静态IP
1.首先查看一下centos7的网卡名称 eno33554984 2.更改为centos7之前版本的网卡名称 3.更改网卡文件的名称 4.禁用可预测命名规则. 通过编辑 /etc/default/gr ...
- 将tiff文件转化为jpg文件并保存
jar包准备 jai-codec和jai-core 主要过程 private boolean parseTifFile(FileItem item) { logger.info("----- ...
- SD--批量删除订单
SD--批量删除订单 在sap应用中常常会需要批量删除一些错误录入的单据,为此开发了一个小程序.该程序为了安全,程序做了一下控制 1.限制用户只能删除自己的订单,不能删除别人输入的订单,如果需要修改一 ...
- HDU 4323 Magic Number(编辑距离DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...
- React-navigation物理返回键提示效果BackHandler
componentWillMount(){ BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid); } co ...
- 【BZOJ】1831: [AHOI2008]逆序对
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...