VBA code for SUMIFS?

后端 未结 3 1598
眼角桃花
眼角桃花 2020-12-22 02:34

I\'m trying to write a custom function that will let me retrieve a cell from the first row in a range that meets x number of criteria. I imagine this would be very similar t

3条回答
  •  一个人的身影
    2020-12-22 03:03

    An example using ADO.

    strFile = Workbooks(1).FullName
    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    cn.Open strCon
    
    'I want to be able to write a function that will give me the value '
    'in column Z where columns W=a, X=b, Y>=7 '
    '(in other words the value 2).' 
    
    strSQL = "SELECT Top 1 Z " _
             & "FROM [Sheet1$] " _
             & "WHERE W='a' And X='b' And Y>=7"
    
    rs.Open strSQL, cn
    
    Result = rs.Fields("Z")
    

提交回复
热议问题