[英]Can't determine range rows count
I have 3 comboboxes placed on a worksheet named cbo1, cbo2, and cbo3. 我在名为cbo1,cbo2和cbo3的工作表上放置了3个组合框。 When the user clicks an element from one, my code retrieves data from a database and places the results in a range of cells below the cbo.
当用户单击一个元素中的一个元素时,我的代码从数据库中检索数据,并将结果放在cbo下方的一系列单元格中。
The Change event of each cbo sends its number (1, 2, or 3) to the routine below to identify which cbo that was clicked as well as its associated range (module level public variables). 每个cbo的Change事件将其编号(1、2或3)发送到下面的例程,以识别单击了哪个cbo及其关联的范围(模块级公共变量)。 Those ranges are initialized in the Workbook_Open routine to an initial range of one cell, the first cell immediately below each cbo.
这些范围在Workbook_Open例程中初始化为一个单元格的初始范围,即每个cbo下方的第一个单元格。 That range will expand and shrink (varying rows, one column) depending on the results of the data retrieval.
该范围将根据数据检索的结果而扩大和缩小(行,一列)。
In the routine, I create a routine-level version of the chosen cbo and its range, do my work, then preserve the range size in the module-level range variables for the next time we come back into the routine. 在例程中,我创建所选cbo及其范围的例程级版本,进行工作,然后将范围大小保留在模块级范围变量中,以备下次使用该例程时使用。 We need to know the range size from the last time so we can first clear it before putting new data into it.
我们需要从上次知道范围大小,以便我们可以先清除它,然后再将新数据放入其中。
Here is my problem: 这是我的问题:
I resize rngCBO (local variable) to hold arrResults. 我调整rngCBO(局部变量)的大小以容纳arrResults。 In doing so, the data is properly displayed in the appropriate cells.
这样,数据将正确显示在适当的单元格中。 However, rngCBO.rows.count always reads as 1, no matter how many rows are actually in the range.
但是,rngCBO.rows.count始终读取为1,无论范围中实际上有多少行。 (You can see I've placed a msgbox immediately after the resize to check it.) This presents a problem for the module-level variables rngCBO1, rngCBO2, and rngCBO3, because when we come back to this subroutine later, the first thing it is supposed to do is rngCBO.ClearContents.
(您可以看到我在调整大小后立即放置了一个msgbox进行检查。)这给模块级变量rngCBO1,rngCBO2和rngCBO3带来了问题,因为稍后再返回此子例程时,第一件事是应该做的是rngCBO.ClearContents。 But since the rows always equals 1, only the first cell below the cbo's is getting cleared.
但是由于行始终等于1,因此仅清除了cbo下方的第一个单元格。 Any cells below that which contain data are not getting cleared.
低于该值的任何包含数据的单元格都不会清除。
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)
doesn't resize rngCBO
. rngCBO.Resize(intRows, 1)
不会调整rngCBO
大小。 It just returns a range that you utilize in that line of code, as you have by assigning an array to that returned range. 它只是返回在该行代码中使用的范围,就像通过将数组分配给该返回的范围一样。 To actually change the extent of rngCBO, you'd do:
要实际更改rngCBO的范围,您可以执行以下操作:
Set rngCBO=rngCBO.Resize(intR, 1)
It's akin to the fact to referring to a Long
called x
like MsbBox x * 2
doesn't change the value of x
. 类似于事实,就像
MsbBox x * 2
那样引用Long
称为x
并不会改变x
的值。 To do that you'd have to say x = x * 2
. 为此,您必须说
x = x * 2
。
Interestingly (perhaps) the ListObject
has a Resize method that actually resizes it. 有趣的是(也许)
ListObject
有一个Resize方法,实际上可以调整它的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.