我试图使用SQL查询来处理我的excel表格作为表格。我写了以下代码:
Global objConn As ADODB.Connection
Global ConnString As String
Global SQL As String
Global objRS As ADODB.Recordset
Global masterFile As String
Public Sub XL_DB_connect()
Set objConn = New ADODB.Connection
masterFile = ThisWorkbook.Path & Application.PathSeparator & ThisWorkbook.Name
ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & masterFile & ";" & _
"Extended Properties=""Excel 12.0;" & _
"HDR=Yes;"";"
objConn.ConnectionString = ConnString
objConn.Open
End Sub
Public Sub executeQuery()
objRS.Source = SQL
objRS.ActiveConnection = objConn
objRS.Open
End Sub
Public Sub XL_DB_relMem()
Set objRS = Nothing
Set objConn = Nothing
SQL = vbNullString
End Sub
Public Sub test()
Set objRS = New ADODB.Recordset
objRS.CursorLocation = adUseClient
SQL = "select PatientGID, count(LOT) from [Sheet1$] group by PatientGID"
Debug.Print SQL
Call XL_DB_connect
Call executeQuery
objRS.MoveFirst
Range("Output").Resize(10, 2).ClearContents
Range("Output").CopyFromRecordset objRS
End Sub
Sheet1具有从单元格A1开始的以下列
patientGID
级数
的IoT
newLoT
loTFdate
actualRegimen
loTRegimenClass
progressionClass
pERMetFlag
在运行代码时,我收到以下错误:
“运行时错误'-2147467259(80004005)'
'Sheet1 $'不是有效名称。确保它不包括 无效的字符和标点符号,并且它不会太长“
答案 0 :(得分:0)
我今天遇到了同样的错误。我发现原因是excel文件处于只读状态。当我将其保存在另一个位置时,该错误立即得到解决。