Excel 返回按指定大小调整的范围。缺少的步骤是将范围设置为返回的范围,如set rngCBO=rngCBO.Resize(intR,1)。这就像引用x*2不会加倍x,除非你这样做x=x*2。我想我已经回答了,但我没有看到我的评论。我真的希望我不会因为发布这篇文章而
Excel 返回按指定大小调整的范围。缺少的步骤是将范围设置为返回的范围,如set rngCBO=rngCBO.Resize(intR,1)。这就像引用x*2不会加倍x,除非你这样做x=x*2。我想我已经回答了,但我没有看到我的评论。我真的希望我不会因为发布这篇文章而,excel,vba,count,range,rows,Excel,Vba,Count,Range,Rows,返回按指定大小调整的范围。缺少的步骤是将范围设置为返回的范围,如set rngCBO=rngCBO.Resize(intR,1)。这就像引用x*2不会加倍x,除非你这样做x=x*2。我想我已经回答了,但我没有看到我的评论。我真的希望我不会因为发布这篇文章而显得多余。道格,你完全正确。添加一行带有Set语句的代码正是我们所需要的。这是一个很好的解释(x*2的例子)。这让我觉得有点傻,但我过去只使用VB6编写代码,所以Excel的范围对我来说是新的。道格,我无法告诉你我有多感激你的帮助。谢谢。感觉有
返回按指定大小调整的范围。缺少的步骤是将范围设置为返回的范围,如
set rngCBO=rngCBO.Resize(intR,1)
。这就像引用x*2
不会加倍x
,除非你这样做x=x*2
。我想我已经回答了,但我没有看到我的评论。我真的希望我不会因为发布这篇文章而显得多余。道格,你完全正确。添加一行带有Set语句的代码正是我们所需要的。这是一个很好的解释(x*2的例子)。这让我觉得有点傻,但我过去只使用VB6编写代码,所以Excel的范围对我来说是新的。道格,我无法告诉你我有多感激你的帮助。谢谢。感觉有点傻是一个很好的方式:)。我很高兴能帮上忙。
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