无法确定范围行数

时间:2014-07-15 22:17:56

标签: excel vba count range rows

我在名为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

1 个答案:

答案 0 :(得分:0)

rngCBO.Resize(intRows, 1)未调整rngCBO的大小。它只返回您在该行代码中使用的范围,就像通过为返回的范围分配数组一样。要实际更改rngCBO的范围,您需要:

Set rngCBO=rngCBO.Resize(intR, 1)

这类似于引用名为Long的{​​{1}} x并不会更改MsbBox x * 2的值。要做到这一点,你必须说x

有趣的是(也许)x = x * 2有一个实际调整大小的Resize方法。

相关问题