|
- Function 目标值(ByVal vModel As Variant, ByVal vType As Variant) As Variant
- '用法:在单元格D2内录入 =目标值(B2,C2)
- 'vModle,型号单元格的值;vType,规格单元格的值
- Dim sModel As String, sType As String
- Dim oConn As Object, oRS As Object
- Dim sSelect As String
-
- sModel = Trim(UCase(vModel))
- sType = Trim(vType)
- If Not (sModel = "YJV" Or sModel = "ZR-YJV") Then
- 目标值 = "错误参数!"
- Exit Function
- End If
-
- Set oConn = CreateObject("Adodb.Connection")
- 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=NO';Data Source=" & ThisWorkbook.Path & "\数据源.xlsx"
- End If
-
- If sModel = "YJV" Then
- sSelect = "Select [F2] From [Sheet1$] Where [F1]='" & sType & "'"
- Else
- sSelect = "Select [F3] From [Sheet1$] Where [F1]='" & sType & "'"
- End If
- Set oRS = CreateObject("Adodb.RecordSet")
- On Error GoTo CloseRead
- Set oRS = oConn.Execute(sSelect)
- If oRS.State = 1 Then
- If Not (oRS.EOF And oRS.BOF) Then vModel = oRS.GetRows
- End If
- oRS.Close
- CloseRead:
- On Error GoTo 0
- Set oRS = Nothing
- oConn.Close
- Set oConn = Nothing
- If IsArray(vModel) Then
- If IsNull(vModel(0, 0)) Then
- 目标值 = "空值"
- Else
- 目标值 = vModel(0, 0)
- End If
- Else
- 目标值 = "没有记录"
- End If
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|