Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从ms access表如何使用vba将所需数据表单数组(getrows)粘贴到excel特定范围_Excel_Vba - Fatal编程技术网

从ms access表如何使用vba将所需数据表单数组(getrows)粘贴到excel特定范围

从ms access表如何使用vba将所需数据表单数组(getrows)粘贴到excel特定范围,excel,vba,Excel,Vba,我下面的代码在运行时没有显示错误,但我不知道如何将必需/特定字段值提取到excel工作表中 Sub getdatafromaccesstoanarray() Dim cn As Object 'Connection Dim rs As Object 'Recordset Dim vAry() As Variant 'Variant Array Dim dbPath As String 'Database Path D

我下面的代码在运行时没有显示错误,但我不知道如何将必需/特定字段值提取到excel工作表中

Sub getdatafromaccesstoanarray()

    Dim cn      As Object   'Connection
    Dim rs      As Object   'Recordset
    Dim vAry()  As Variant  'Variant Array
    Dim dbPath  As String   'Database Path
    Dim dbName  As String   'Database Name
    Dim txt     As String

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    dbPath = ThisWorkbook.Path & "\"
    dbName = "NewDB.accdb"

    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & dbPath & dbName & ";"

    rs.Open "SELECT * FROM BILLDETAILS WHERE BILLDETAILS.SN_AUTO =100;", cn

    vAry = rs.GetRows()

    'now when the data is copied to my array how can i paste specific values from this data to
    'cells in my excel sheet
    'like
    'on active sheet
    '[a1] = vAry(value1)
    '[a2] = vAry(value3)
    '[a3] = vAry(value8)

    'and other values like wise


    rs.Close
    cn.Close

    Set rs = Nothing
    Set cn = Nothing
End Sub
如果有任何其他方法可以做到这一点,那么请让我知道。
谢谢

如果只想将记录集复制到工作表中,可以使用CopyFromRecordset方法通过指定左上角将表转储到工作表中:

 Range("a1").copyfromrecordset rs
如果要将特定字段放在特定位置,可以循环

Do While not rs.eof
   range("a2")=rs(0)
   range("b2")=rs(1)
    'etc....
  rs.movenext
Loop

您需要找到数组的UBound和Excel工作表上的最后一行数据,然后使用for循环写入Excel的下一个空行……谢谢,先生,它工作得很好。我非常感谢你。