简体   繁体   English

如何在Excel工作表中的命名范围上运行SQL语句?

[英]How can I run SQL statements on a named range within an excel sheet?

All I am trying to do is take a standard range on an excel sheet (ie a named range, or even A1:F100), and run some sql queries on it, and return a recordset that I can either step through in VBA code, or even just paste into some other sheet in the same workbook. 我要做的就是在Excel工作表(即命名范围,甚至A1:F100)上采用标准范围,并在其上运行一些SQL查询,并返回一个我可以在VBA代码中单步执行的记录集,或者甚至只是粘贴到同一工作簿中的其他工作表中。

Using ADODB was one thought, but how could I setup the connectionstring to point to some range within the current workbook? 使用ADODB是一个想法,但我怎么能设置connectionstring指向当前工作簿中的某个范围?

I know before I have made use of the Microsoft query wizard, which was not ideal, but would work. 我知道在使用Microsoft查询向导之前,这不是理想的,但可以使用。 I can't seem to get this to refer to a range within the sheet, only other excel files. 我似乎无法将此引用到工作表中的范围,只能引用其他excel文件。


Here is the function I am left with. 这是我留下的功能。 When I run it a few times my excel crashes with the usual out of resources error message. 当我运行它几次我的excel崩溃与通常的资源外错误消息。 I have removed this function from my spreadsheet, and everything runs seamlessly multiple times, thus it is definitely caused by the code here. 我已从电子表格中删除了此功能,并且所有内容都无缝运行多次,因此肯定是由此处的代码引起的。

I have cleaned up all the objects (correctly?). 我已经清理了所有物体(正确吗?)。 Does anyone have any ideas what could be going wrong? 有没有人有什么想法会出错? Could there be something in the connection string that could be tweaked, or could it be something to do with the variant that is returned from the GetRows method? 连接字符串中是否存在可以调整的内容,或者它可能与GetRows方法返回的变量有关?

I am using MS ADO 2.8, and have also tried 2.5 with the same behaviour. 我正在使用MS ADO 2.8,并且也尝试过2.5具有相同的行为。

Function getTimeBuckets() As Collection

Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim dateRows As Variant
Dim i As Integer
Dim today As Date

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

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

strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strCon

strSQL = "SELECT DISTINCT(Expiration) FROM [PositionSummaryTable] where [Instrument Type] = 'LSTOPT'" 

rs.Open strSQL, cn


dateRows = rs.GetRows
rs.Close

'today = Date
today = "6-may-2009"

For i = 1 To UBound(dateRows, 2)
    If (dateRows(0, i) >= today) Then
        getTimeBuckets.Add (dateRows(0, i))
    End If
Next i

Set dateRows = Nothing
Set cn = Nothing
Set rs = Nothing
End Function

You can just use the name. 你可以使用这个名字。

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset 

strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

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

cn.Open strCon

''Pick one:
strSQL = "SELECT * FROM DataTable" ''Named range
strSQL = "SELECT * FROM [Sheet1$A1:E346]" ''Range

rs.Open strSQL, cn

Debug.Print rs.GetString

In response to question part 2 回答问题第2部分

I notice that you only want today's records, so you should be able to modify the sql to: 我注意到你只想要今天的记录,所以你应该能够将sql修改为:

strSQL = "SELECT DISTINCT(Expiration) FROM [PositionSummaryTable] " _
& "where [Instrument Type] = 'LSTOPT' AND [Expiration]=#" _
& Format(Date(),"yyyy/mm/dd") & "#"

You have not closed the connection: 您尚未关闭连接:

cn.Close

And then 然后

 Set rs=Nothing
 Set cn=Nothing

How about using of LIKE clause? 如何使用LIKE子句?

I tried to use: 我试着用:

select * from [PES$] where PID_TAG like '*5400001'

without success.... 没有成功....

this works: 这工作:

select * from [PES$] where PID_TAG = 'PIT5400001'

but this is not I want. 但这不是我想要的。

EDIT 编辑

hummm.... we need to change wildcards to work... don't use *, use % 嗯....我们需要改变通配符才能工作......不要使用*,使用%

If you are using powershell, $ is an internal character. 如果您使用的是powershell,则$是一个内部字符。 use `$ 用'$

eg: 例如:

"Select * from [VM`$A3:F30]"

I don't know about VBA, but there are code online in Delphi and C# that uses the format 我不知道VBA,但Delphi和C#中有使用该格式的代码

SELECT * FROM [SheetName$A1:B2]

Have you tried it? 你试过吗?

You will find all You need below: 你会在下面找到你需要的所有东西:

Each link is for VBA 每个链接都适用于VBA

One

Two

Three

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM