码迷,mamicode.com
首页 > 编程语言 > 详细

利用Excel VBA实现批量数据分组转置

时间:2015-03-10 21:34:29      阅读:677      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

问题:如上图所示,按lon,lat分组,再进行转置。

Sub admin()
    Dim conn, xRs, xFd
    Set conn = CreateObject("ADODB.Connection")
    conn.Open "Provider=Microsoft.Jet.Oledb.4.0;" & _
              "Extended Properties= 'Excel 8.0;hdr=yes;IMEX=1' ;" & _
              "Data Source=" & ThisWorkbook.FullName
    Set xRs = CreateObject("ADODB.RecordSet")
        sSql = " Transform Sum( [Tas_t] ) Select [lon] , [lat] From [Sheet1$A:D] Group By [lon] , [lat] Pivot [Year] "
        xRs.Open sSql, conn, 1, 3
        i = 0
        For Each xFd In xRs.Fields
            Range("F1").Offset(0, i) = xFd.Name
            i = i + 1
        Next
        Range("F2").CopyFromRecordset xRs
        xRs.Close
        conn.Close
    Set xRs = Nothing
    Set conn = Nothing
End Sub


利用Excel VBA实现批量数据分组转置

标签:

原文地址:http://blog.csdn.net/lucky51222/article/details/44180303

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!