gpt4 book ai didi

excel - vba listobject CopyFromRecordset

转载 作者:行者123 更新时间:2023-12-02 11:09:30 26 4
gpt4 key购买 nike

如何将记录集中的数据填充到列表对象中?
下面的代码不完全工作:

oCN.ConnectionString = "DRIVER={SQL Server};Server=SRVSQL;Database=TEST;"
oCN.Open
Dim sqlString As String
sqlString = "SELECT * FROM MYTABLE"
oRS.Open sqlString, oCN

With Feuil3.ListObjects("TableArticles")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If

' This make a 91 error
Call .DataBodyRange.CopyFromRecordset(oRS)
' This copy data into sheet, not into listobject
Call Feuil3.Range("A2").CopyFromRecordset(oRS)
End With

最佳答案

如果您总是删除 .DataBodyRange首先,您可以使用“.InsertRowRange”。

With Feuil3.ListObjects("TableArticles")
If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete
.InsertRowRange.CopyFromRecordset(oRS)
End With

关于excel - vba listobject CopyFromRecordset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38008306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com