簡體   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