繁体   English   中英

无法确定范围行数

[英]Can't determine range rows count

我在名为cbo1,cbo2和cbo3的工作表上放置了3个组合框。 当用户单击一个元素中的一个元素时,我的代码从数据库中检索数据,并将结果放在cbo下方的一系列单元格中。

每个cbo的Change事件将其编号(1、2或3)发送到下面的例程,以识别单击了哪个cbo及其关联的范围(模块级公共变量)。 这些范围在Workbook_Open例程中初始化为一个单元格的初始范围,即每个cbo下方的第一个单元格。 该范围将根据数据检索的结果而扩大和缩小(行,一列)。

在例程中,我创建所选cbo及其范围的例程级版本,进行工作,然后将范围大小保留在模块级范围变量中,以备下次使用该例程时使用。 我们需要从上次知道范围大小,以便我们可以先清除它,然后再将新数据放入其中。

这是我的问题:
我调整rngCBO(局部变量)的大小以容纳arrResults。 这样,数据将正确显示在适当的单元格中。 但是,rngCBO.rows.count始终读取为1,无论范围中实际上有多少行。 (您可以看到我在调整大小后立即放置了一个msgbox进行检查。)这给模块级变量rngCBO1,rngCBO2和rngCBO3带来了问题,因为稍后再返回此子例程时,第一件事是应该做的是rngCBO.ClearContents。 但是由于行始终等于1,因此仅清除了cbo下方的第一个单元格。 低于该值的任何包含数据的单元格都不会清除。

Option Explicit

'Module variables
Public rngCBO1 As Range
Public rngCBO2 As Range
Public rngCBO3 As Range

Public Sub WBcboChange(intNum As Integer)

    Dim cboObj As ComboBox
    Dim rngCBO As Range
    Dim intR As Integer
    Dim intRows As Integer
    Dim strSQL As String
    Dim intFld As Integer
    Dim strFld As String
    Dim intChoice As Integer
    Dim rstResults As ADODB.Recordset
    Dim arrResults() As Date

    Select Case intNum  'Determine which cbo has been clicked (intNum brings it in)
        Case 1
            Set cboObj = wsStats.cboWB1
            Set rngCBO = rngCBO1
        Case 2
            Set cboObj = wsStats.cboWB2
            Set rngCBO = rngCBO2
        Case 3
            Set cboObj = wsStats.cboWB3
            Set rngCBO = rngCBO3
    End Select

    'clear any residual data from the cells under the cbo
    rngCBO.ClearContents

    If cboObj.Text <> "(none)" Then 'the user might just want to clear the cells and not be asking for more data

        intChoice = CInt(cboObj.Text)   'the cbo contains integer choices

    'Build the Fields string
        For intFld = 1 To 5  'there are five fields, each name being identical except ending in 1 through 5
            strFld = strFld & "tblAllDAta.[fld" & intFld & "] = " & intChoice
            If intWB < 5 Then
                strFld = strFld & " OR "
            End If
        Next

        'the data being retrieved consists of dates
        strSQL = "SELECT tblAllDAta.[Date] " & _
            "FROM tblAllDAta " & _
            "WHERE " & strFld & _
            " ORDER BY tblAllDAta.[Date] DESC"

        OpenDB  'call the routine which opens the dB connection
            Set rstResults = GetReadOnlyRecords(strSQL) 'call the function that acquires the desired records
            intRows = rstResults.RecordCount
            If intRows > 0 Then

                'transfer data from the recordset into an array
                ReDim arrResults(1 To intRows, 1 To 1)
                intR = 1
                rstResults.MoveFirst
                Do While Not rstResults.EOF
                    arrResults(intR, 1) = rstResults("Date")
                    rstResults.MoveNext
                    intR = intR + 1
                Loop

                'THIS IS WHERE MY PROBLEM OCCURS (I think)
                rngCBO.Resize(intRows, 1) = arrResults
                MsgBox rngCBO.Rows.Count   'this is always 1 !!!!!!!!!!!!!!!!

            Else
                'there were no records matching the query
                rngCBO.Resize(1, 1) = "Never"

            End If

            Set rstResults = Nothing
        CloseDB

    End If

    'preserve the new ranges
    Select Case intNum
        Case 1
            Set rngCBO1 = rngCBO
        Case 2
            Set rngCBO2 = rngCBO
        Case 3
            Set rngCBO3 = rngCBO
    End Select

    Set rngCBO = Nothing
    Set cboObj = Nothing

End Sub

rngCBO.Resize(intRows, 1)不会调整rngCBO大小。 它只是返回在该行代码中使用的范围,就像通过将数组分配给该返回的范围一样。 要实际更改rngCBO的范围,您可以执行以下操作:

Set rngCBO=rngCBO.Resize(intR, 1)

类似于事实,就像MsbBox x * 2那样引用Long称为x并不会改变x的值。 为此,您必须说x = x * 2

有趣的是(也许) ListObject有一个Resize方法,实际上可以调整它的大小。

暂无
暂无

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

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