|
- Sub test()
- '用法:在单元格D2内录入 =目标值(B2,C2)
- 'vModle,型号单元格的值;vType,规格单元格的值
- Dim sModel As String, sType As String
- Dim oConn As Object, oRS As Object
- Dim sSelect As String
- Dim i As Long
- Dim vModel As Variant
-
- Set oConn = CreateObject("Adodb.Connection")
- Set oRS = CreateObject("Adodb.RecordSet")
-
- If Val(Application.Version) < 12 Then
- oConn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties='Excel 8.0;IMEX=0;HDR=NO';Data Source=" & ThisWorkbook.Path & "\数据源.xlsx"
- Else
- oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;IMEX=0;HDR=YES';Data Source=" & ThisWorkbook.Path & "\数据源.xlsx"
- End If
-
- For i = 2 To 9
- sModel = Cells(i, 2).Value
- sType = Cells(i, 3).Value
-
- sSelect = "Select [" & sModel & "] From [Sheet1$A2:C] Where [规格] = '" & sType & "'"
- If oRS.State = 1 Then oRS.Close
-
- oRS.Open sSelect, oConn, 3, 1
-
- If oRS.RecordCount > 0 Then
- Cells(i, 4).Value = oRS.Fields(sModel).Value
- Else
- Cells(i, 4).Value = "没有记录"
- End If
- Next
-
- Set oRS = Nothing
- oConn.Close
- Set oConn = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|