writing copyfromrecordset to range

前端 未结 1 906
面向向阳花
面向向阳花 2021-01-27 11:35

I\'ve got the following vba, it reads in the MCO from cell C10 onwards until its empty and will grab the number of machines, number of decrypts and upgrading machines from a SQL

相关标签:
1条回答
  • 2021-01-27 11:59

    It might be easiest to include the Excel sheet as a joined table. For example:

    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    ''Not the best way to get the name
    strFile = ActiveWorkbook.FullName
    
    ''2007 / 2010 connection
    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 12.0 xml;HDR=Yes;"";"
    
    cn.Open strCon
    
    ''ODBC Connection for sql server
    scn = "[ODBC;DRIVER=SQL Server;SERVER\Instance;" _ 
        & "Trusted_Connection=Yes;DATABASE=Test]"
    
    sSQL = "SELECT a.Stuff, b.ID, b.AText FROM [Sheet5$] a " _
    & "INNER JOIN " & scn & ".table_1 b " _             
    & "ON a.Stuff = b.AText"
    rs.Open sSQL, cn
    
    ActiveWorkbook.Sheets("Sheet7").Cells(1, 1).CopyFromRecordset rs
    

    With any links to SQL Server, you need to be fairly confident that you are working with clean data.

    Note that I have referred to Cells. If you do not like the idea of connecting the sheet, you can also refer to cells and step, for example For i=1 To MaxRows

    0 讨论(0)
提交回复
热议问题