为什么使用命名范围不返回任何值?

发布于 2024-09-01 02:16:43 字数 1033 浏览 1 评论 0原文

我有一本包含几张纸的工作簿。我填充第二张表上的列表框(从单元格中提取静态数据),单击按钮,它运行良好。

当我使用命名范围填充列表框时,列表框会按照我想要的方式填充,但我收到错误,因为代码认为我没有选择列表框中的任何内容,即使我选择了。因此它通过 "" 而不是 "title"

这是一个常见问题吗?命名范围不是问题,因为它将数据传递到列表框,并且我知道它正在选择数据,因为一旦列表框失去焦点,它就会将单元格的内容吐出到单元格 A1

更奇怪的是,我将列表框的内容设置为 Msg1。因此,A1 填充了 Msg1(我在列表框中实际选择的内容)。但是当我尝试在代码中使用 Msg1 时,它告诉我 Msg1""。同样,这种情况仅在我使用动态命名范围时发生,而不是在单元格 K1:K9 中使用静态数据时发生。

Private Function strEndSQL1 As String

Dim strSQL As String

    strSQL = ""
            'Create SQL statement
        strSQL = "FROM (SELECT * FROM dbo.Filter WHERE ID = " & TextBox1.Text & " And Source IN (" & Msg1 & ")) a FULL OUTER JOIN "
        strSQL = strSQL & "(SELECT * FROM dbo.Filters WHERE ID = " & TextBox2.Text & " And Source IN (" & Msg1 & ")) b "
        strSQL = strSQL & "ON a.Group = b.Group    
    strEndSQL = strSQL

End Function

I have one workbook with several sheets. I populate the listboxes (pulling static data from cells) on the 2nd sheet, click a button and it runs fine.

When I populate the listboxes with a named range, the listbox populates the way I want, but I get an error because the code thinks that I didn't select anything in the listbox, even though I did. So it passes through "" instead of "title".

Is this a common issue? The named range isn't a problem because it passes through the data to the listbox and I know it's selecting data because as soon as the listbox loses focus, it spits out the contents of the cell into cell A1.

What's even stranger is that I have the contents of the listbox set to Msg1. So A1 gets populated with Msg1 (what I actually selected in the listbox). But when I try and use Msg1 in the code, it tells me that Msg1 is "". Again, this only happens when I use the dynamic named range, not with static data in cells K1:K9.

Private Function strEndSQL1 As String

Dim strSQL As String

    strSQL = ""
            'Create SQL statement
        strSQL = "FROM (SELECT * FROM dbo.Filter WHERE ID = " & TextBox1.Text & " And Source IN (" & Msg1 & ")) a FULL OUTER JOIN "
        strSQL = strSQL & "(SELECT * FROM dbo.Filters WHERE ID = " & TextBox2.Text & " And Source IN (" & Msg1 & ")) b "
        strSQL = strSQL & "ON a.Group = b.Group    
    strEndSQL = strSQL

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-09-08 02:16:43

我不确定您如何填充列表框,或者列表框是来自表单工具栏还是控件工具箱。如果是后者,下面是填充和检索值的示例。

Sub FillListBox()

    Sheet1.ListBox1.List = Sheet1.Range("MyNamedRange").Value

End Sub

Sub MakeSQL()

    Dim sSql As String
    Dim Msg1 As String

    With Sheet1.ListBox1
        If .ListIndex > -1 Then
            Msg1 = .Value
        End If
    End With

    sSql = "SELECT * FROM MyTable WHERE ID=" & Msg1

    Debug.Print sSql

End Sub

请注意,Value 属性将取决于您在 BoundColumn 属性中设置的内容。

I'm not sure how you're filling the listbox, or whether the listbox is from the Forms toolbar or the Control Toolbox. If it's the latter, here's an example for populating and retrieving values.

Sub FillListBox()

    Sheet1.ListBox1.List = Sheet1.Range("MyNamedRange").Value

End Sub

Sub MakeSQL()

    Dim sSql As String
    Dim Msg1 As String

    With Sheet1.ListBox1
        If .ListIndex > -1 Then
            Msg1 = .Value
        End If
    End With

    sSql = "SELECT * FROM MyTable WHERE ID=" & Msg1

    Debug.Print sSql

End Sub

Note that the Value property will depend on what you've set in the BoundColumn property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文