您可以使用 Excel 和 ADO 做很多事情。
Dim cn As Object
Dim rs As Object
Dim sFile As String
Dim sCn As String
Dim sSQL As String
Dim s As String, f As String
Dim sa As Variant
Dim i As Integer, c As Integer
Dim r As Range
''This is not the best way to refer to the workbook
''you want, but it is very conveient for notes
''It is probably best to use the name of the workbook.
sFile = ActiveWorkbook.FullName
sCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open sCn
sSQL = "SELECT interests " _
& "FROM [Sheet1$] "
rs.Open sSQL, cn, 3, 3
With Worksheets("Sheet2")
s = rs.GetString(, , , ",")
sa = Split(s, ",")
c = 1
For i = 0 To UBound(sa)
Set r = .Range("a1:a" & c)
f = Trim(sa(i))
If r.Find(f) Is Nothing Then
c = c + 1
.Cells(c, 1) = f
End If
Next
.Cells(1, 1) = "Interests"
.Cells(1, 2) = "Male"
.Cells(1, 3) = "Female"
For i = 2 To c
rs.Close
sSQL = "SELECT Gender, Count(Gender) As GNo " _
& "FROM [Sheet1$] " _
& "WHERE Interests Like '%" & .Cells(i, 1) & "%' " _
& "GROUP BY Gender"
rs.Open sSQL, cn
Do While Not rs.EOF
If rs.Fields("Gender") = "Male" Then
.Cells(i, 2) = rs.Fields("GNo")
ElseIf rs.Fields("Gender") = "Female" Then
.Cells(i, 3) = rs.Fields("GNo")
End If
rs.MoveNext
Loop
Next
End With
''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub