简体   繁体   English

Excel单元格条件的条件SQL语句

[英]condition sql statement with excel cell reference

I am applying some SQL query into a spreadsheet, and have trouble to minimize number of worksheets/connections. 我正在将一些SQL查询应用到电子表格中,但无法最大限度地减少工作表/连接数。

i have 3 scenarios, for each of them, I use same query 我有3个场景,对于每个场景,我都使用相同的查询

 select *
 from ***scenario_table***
 where mycolumn > 100

Now I have to do this three times for different "scenario_table", and I really wanna to use a cell as reference (let's say cell $A$1) 现在,我必须针对不同的“ scenario_table”执行三遍此操作,并且我真的想使用单元格作为参考(假设单元格$ A $ 1)

I wanna sth like this 我想这样

 select *
 from
     case when [$A$1] = 1 ***scenario1_table***
     case when [$A$1] = 2 ***scenario2_table***
     case when [$A$1] = 3 ***scenario3_table***
  where mycolumn > 100

my I know whether there is any way to work it out? 我知道是否有办法解决?

Thanks 谢谢

This solution uses . 此解决方案使用

Assuming your cell with content is in Sheet1!A1 and there is only one conecction in your workbook, insert following code in Sheet1 : 假定包含内容的单元格位于Sheet1!A1并且工作簿中只有一个概念,请在Sheet1插入以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("A1")) Then
        Select Case Me.Range("A1").Value
            Case 1: SetTable ("Scenary1_Table")
            Case 2: SetTable ("Scenary2_Table")
            Case 3: SetTable ("Scenary3_Table")
        End Select
    End If
End Sub

Sub SetTable(ByVal TableName As String)
    With ThisWorkbook.Connections(1)
        .ODBCConnection.CommandText = "SELECT * FROM " & TableName
        .Refresh
    End With
End Sub

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

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