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

  

时间: 2024-11-09 00:20:50

20170921xlVBA_SQL蒸发循环查询2的相关文章

循环查询sql带逗号(,)分隔的数据

? 1 2 3 4 5 6 select * from sys_role_list where id in (select c from (with test as (select roleid c from sys_role_info where id=1) select substr(t.ca,instr(t.ca, ',', 1, c.lv) + 1,instr(t.ca, ',', 1, c.lv + 1) - (instr(t.ca, ',', 1, c.lv) + 1)) AS c

SqlServer中循环查询结果集

declare @id int,@value nvarchar(100); begin declare c_test_main cursor fast_forward for select hps_hpId,hps_time from hospitalPermSupp where hps_usId=1; open c_test_main;--打开游标 while 1=1 --开始循环 begin fetch next from c_test_main into @id,@value; --赋值到

2017-09-21xlVBA_蒸发SQL循环查询1

'ARRAY("1991","1992","1993","1994","1996","1997","1998","1999","2001") Sub ADO_SQL_QUERY_ONE_RNG() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAler

for循环查询,导致最后返回的list都是最后一个对象的问题

for循环中有相同的id,通过id去查询时,会将之前查询到的缓存信息直接返回,导致获取到的对象hashcode值相同(同一对象) 将xml中flushcache属性设置为true就可以解决 或者另一种解决方案 在for循环中new一个对象,将查询到的对象属性拷贝到new的对象里,再加入到list中 原文地址:https://www.cnblogs.com/SeaWxx/p/10011114.html

解决 mybatis 中相互关联的两种表数据在返回前端时一直循环查询,直到StackOverFlow报错

原代码设计: 一个用户有多个账户,是一对多的关系,用Collection集合关联:一个账户独属于一个用户,是一对一的关系,用association来关联: UserMapper.xml <mapper namespace="com.xuetu.springboot.mapper.UserMapper"> <!-- 定义User的resultMap--> <resultMap id="userMap" type="user&qu

[moka同学]Yii2.0循环查询并对结果累加求和

在控制器中查询好数据  $model 在视图中输入 <?php foreach($model as $key=>$r):?> <tr class="text-center"> <td><?=$r->id?></td> <td><?=$r->name?></td> <td><?php echo Province::find()->where(['cou

Oracle循环查询结果集 自定义函数

create or replace function Fun_GetRoleIDList(d_fid char) return varchar is  rolelist varchar(2000);begin  rolelist := '';  ---定义游标 declare cursor role_cur is select a.roleid from u_roleinfo a , u_node_roles b  where  a.roleid=b.roleid and b.fid=d_fid

遍历id,根据id作为条件循环查询。

string id = "OE09924008161405102,OE36765709071405102,OE87852044171405102,OE09924008161405102"; string[] str = id.Split(','); foreach (string item in str) { string sql = @" select * from table  where [email protected]"; } 感谢 C-Sharp 高级开

oracle 游标循环, 查询,异常捕获,继续执行

declare       --类型定义       strsql string(500);        v_ErrorCode NUMBER;           -- Variable to hold the error message code           v_ErrorText VARCHAR2(200);    -- Variable to hold the error message text          cursor c_eventstate       is