Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

sql - associative array in vba like php

I need your help for building analytics in Microsoft Excel using VBA.

I have one data sheet with y columns and n lines

Line Number  Firstname  Lastname  Country  Training Hours  Training Status
1            John       Smith     USA      1               Completed
2            Henri      Dupont    France   1               Completed
3            Paul       Walker    USA      25              Incomplete
4            Ron        Howard    USA      10              Completed

And I would like to use array like in php but using VBA

For i = 1 To nblines
    If(array[i]["Country"] = "USA" And array[i]["Training Status"] = "Completed") Then
        myval = myval + array[i]["Training Hours"]
    End If
Next

myval should be 11

Unfortunately I don't find THE solution. I've tried dictionnary, collection and array but without success. Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can make the SQL query to the worksheet in the opened workbook (the same way as to any other workbook). In this case the query string will be as follows:

SELECT SUM([Training Hours]) AS Myval FROM [data sheet$] WHERE Country = 'USA' AND [Training Status] = 'Completed';

Here is the code

Sub TestSQLRequest()

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1

    Select Case LCase(Mid(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".")))
        Case ".xls"
            strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source='" & ThisWorkbook.FullName & "';Mode=Read;Extended Properties=""Excel 8.0;HDR=YES;"";"
        Case ".xlsm"
            strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source='" & ThisWorkbook.FullName & "';Mode=Read;Extended Properties=""Excel 12.0 Macro;HDR=YES;"";"
    End Select

    With CreateObject("ADODB.Connection")
        .Open strConnection
        With .Execute("SELECT SUM([Training Hours]) AS Myval FROM [data sheet$] WHERE Country = 'USA' AND [Training Status] = 'Completed';")
            Myval = .Fields("Myval")
        End With
        .Close
    End With

    MsgBox Myval

End Sub

Within the query string, the column names with spaces should be put into square brackets, as well as the name of the worksheet containing data followed by $. It goes without saying that the query can't access to the data, which wasn't saved to the file after some changes have been made to the sheet. Note that Excel 8.0 provider won't work on 64-bit Excel version, try to use Excel 12.0 provider instead (the second strConnection assignment).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...