|
本人做了一个EXCEL版的,我还没装SQL server 况且我的系统为英文版的,在SQL server 中要用到游标,思路是先将合同号用Distinct出唯一值,用游标嵌套查询,下个月回中国,到时再看看
双击sheet2工作表单元格执行查询。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim arr, arr2, arr3
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
'Dim intI As Integer, intJ As Integer
Sheet2.Range("A:B").ClearContents
strSQL = "select Distinct ID from [sheet1$]"
Sheet2.Range("A1:B1") = Sheet1.Range("A1:B1").Value
If cnn.State = 0 Then
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;IMEX=1;HDR=YES';Data Source=" & ThisWorkbook.FullName
End If
rst.Open strSQL, cnn, adOpenKeyset, adLockPessimistic
arr = rst.GetRows
Sheet2.Cells(2, 1).Resize(UBound(arr, 2) + 1, UBound(arr, 1) + 1) = Application.Transpose(arr)
rst.Close
ReDim arr2(UBound(arr, 2))
For i = 0 To UBound(arr, 2)
strSQL = "select Item from [sheet1$] where ID=" & arr(0, i)
rst.Open strSQL, cnn, adOpenKeyset, adLockPessimistic
arr3 = rst.GetRows
rst.Close
For j = 0 To UBound(arr3, 2)
arr2(i) = arr2(i) & "," & arr3(0, j)
Next
arr2(i) = Mid(arr2(i), 2, Len(arr2(i)) - 1)
Next
Sheet2.Cells(2, 2).Resize(UBound(arr2) + 1, 1) = Application.Transpose(arr2)
End Sub |
|