VBA将记录集从一个excel转置到另一个excel

时间:2016-01-04 23:10:04

标签: vba excel-vba recordset excel

我有一个excel表格:

        11/1/15  11/2/15  11/3/15  
Type1     1         2        1         row123
Type2     A         C        D         row155

我将在另一个excel中更新:

Public Function connectSS()
Set DBCONT = CreateObject("ADODB.Connection")
Dim strDBPath As String
'Newestfile is a function that I created to locate the newest file on a path 
Call NewestFile("\\Mac\Home\Desktop", "*.xlsx")

strDBPath = Chr(34) & "\\Mac\Home\Desktop\" & NewestFile("\\Mac\Home\Desktop\VBA 2010", "*.xlsx") & Chr(34)

Dim sConn As String
sConn = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBPath & _
                    ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"

On Error GoTo 2015
DBCONT.Open sConn
DBCONT.cursorlocation = 3

Exit Function

Sub Tester()
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
Dim sqlstr As String

'Here is what I got stuck, how do I extract data with the latest date(last column?
sqlstr = ????????

Call connectSS
rs.Open sqlstr, DBCONT

Worksheets("sheet3").Range("a2").CopyFromRecordset rs
Worksheets("sheet3").Columns("A:Q").EntireColumn.AutoFit
rs.Close
Set rs = Nothing

我需要找到最新的日期并将其添加回第二个excel。我不知道如何使用记录集这样做。我很擅长excel所以任何建议都会很好!

以下是我使用的代码:

    w = $(window).width();
    h = $(window).outerHeight();

    // Adaptive Slider
    switch(true) {
        case (w > 1190):
               minimum = 3;
               maximum = 3;
               width = 5000;
               margin = 3;
            infinite = false;
            hide = true;
            currentState = "desktop";
              break;
              case (w < 1190 && w > 767):
               minimum = 2;
               maximum = 2;
               width = 5000;
               margin = 20;
            infinite = false;
            hide = true;
            currentState = "tablet";
              break;
              default:
               minimum = 1;
               maximum = 1;
               width = 5000;
               margin = 0;
            infinite = false;
            hide = true;
            currentState = "mobile";
    }
    var mySettings = {
        touchEnabled: true,
        auto  : false,
        autoHover : false,
        pager : true,
        minSlides: minimum,
        maxSlides: maximum,
        slideWidth: width,
        slideMargin: margin,
        infiniteLoop: infinite,
        hideControlOnEnd: hide,
        onSlideBefore : removePlayer,
        onSlideAfter : removePlayer
    }
    clearTimeout(id);
    id = setTimeout(function(){
        if (!adaptLoaded) {
            $(".bx-adapt").each(function(i) {
                slider["adapt"][i] = $(this).bxSlider(mySettings);
            });
            adaptLoaded = true;
            loadedState = currentState;
        } else if (loadedState != currentState) {
            for (var i = 0; i < slider["adapt"].length; i++) {
                slider["adapt"][i].reloadSlider(mySettings);
            };
            loadedState = currentState;
        }
    }, 500);


    // Tablet Slider
    tabletSettings = {
        touchEnabled: true,
        infiniteLoop: false,
        hideControlOnEnd: true,
        auto  : false,
        autoHover : false,
        pager : true,
        onSlideBefore : removePlayer,
        onSlideAfter : removePlayer
    };
    if (w > 1183){
        if (tabletLoaded){
            for (var i = 0; i < slider["tablet"].length; i++) {
                destroySlider(slider["tablet"][i]);
                slider["tablet"][i].replaceWith(tabletClone[i].clone(true,true));
            };
               tabletLoaded = false;
             }
    } else {
        if (!tabletLoaded){
            $(".bx-tablet").each(function(i) {
                tabletClone[i] = jQuery($(this)).clone(true,true);
                slider["tablet"][i] = $(this).bxSlider(tabletSettings);
            });
            tabletLoaded = true;
        };
    };

    // Mobile Slider
    mobileSettings = {
        touchEnabled: true,
        infiniteLoop: false,
        hideControlOnEnd: true,
        auto  : false,
        autoHover : false,
        pager : true
    };
    if (w > 767){
        if (mobileLoaded){
           for (var i = 0; i < slider["mobile"].length; i++) {
                slider["mobile"][i].destroySlider();
                slider["mobile"][i].replaceWith(mobileClone[i].clone(true, true));
            };
            mobileLoaded = false;
        }
    }else{
        if (!mobileLoaded){
            $(".bx-mobile").each(function(i) {
                mobileClone[i] = jQuery($(this)).clone(true, true);
                slider["mobile"][i] = $(this).bxSlider(mobileSettings);
            });
            mobileLoaded = true;
        }
    };

也许与转置有关?谢谢你的阅读!

我的问题是,由于这是一个转置格式数据,我无法从工作表中执行传统的选择列标题。我不知道如何使用记录集来处理转置数据。这就是我需要的东西帮助我。再次感谢!

1 个答案:

答案 0 :(得分:0)

希望这是一种更优雅的方式,但你可以通过运行两个不同的查询来实现这一目标。

首先,建立最后一个列名:

Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")

rs.Open "SELECT * FROM [Sheet1$];", DBCONT

Dim sFieldName As String
With rs.Fields
    ' Fields is a zero-based collection so the last field is at (count - 1)
    sFieldName = .Item(.Count - 1).Name
End With

rs.Close

现在,将该字段名称合并到查询中。应尽可能使用参数,而不是将查询构建为字符串。 ADO不接受列名或表名的参数,所以我们需要在这种情况下构建一个字符串:

Set rs = CreateObject("ADODB.Recordset")
' Assumption: the first column is needed in the recordset
' As this column doesn't have a name in your example data, it will
' get the place holder name of F1 (i.e. F & column number). If this
' column has a name in the real data then replace F1 with that name
rs.Open "SELECT [F1], [" & sFieldName & "] FROM [Sheet1$];", DBCONT

Dim fld As Object
Dim i As Integer

With Worksheets("Sheet3")
    .UsedRange.ClearContents
    i = 0
    For Each fld In rs.Fields
        i = i + 1
        .Cells(1, i).Value = fld.Name
    Next fld
    .Cells(2, 1).CopyFromRecordset rs
    .Columns("A:Q").EntireColumn.AutoFit

End With

rs.Close