|
原帖由 yuliangexcel 于 2011-7-31 18:14 发表
当查询条件在数据源中不存在时,提示下标越界,版主再给改一下。
Private Sub CommandButton1_Click()
Dim cnn As Object, rs As Object, SQL$, arr(), i&, j&, m&
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=Excel 8.0;data source=" & ThisWorkbook.FullName
SQL = "select 抗压强度 from [Sheet1$" & Sheets("Sheet1").[a1].CurrentRegion.Address(0, 0) & "] where 配合比编号='" & [N4] & "' and 供货时间 between #" & [N5] & "# and #" & [R5] & "#"
Set rs = CreateObject("adodb.Recordset")
rs.Open SQL, cnn, 1, 3
ActiveSheet.UsedRange.Offset(15).ClearContents
If rs.RecordCount > 0 Then
ReDim arr(1 To rs.RecordCount, 1 To 20)
For i = 1 To rs.RecordCount
m = Int((i - 1) / 10) + 1
j = i Mod 10
If j = 0 Then j = 10
arr(m, (j - 1) * 2 + 1) = rs.Fields(0).Value
rs.MoveNext
Next
[a16].Resize(m, 20) = arr
Else
MsgBox "没有查到", vbInformation
End If
cnn.Close
Set cnn = Nothing
End Sub |
评分
-
1
查看全部评分
-
|