简体   繁体   中英

Excel VBA Aggregate Function Error

I am new to Excel VBA. When I run the below code, I get an error "missing operator in query expression sum[Project Hrs]". What am I doing wrong?

Sub TaskHrs()

   strSQL = "Select [User Name], [Task Name], sum[Project Hrs] from [idata$]      group by [User Name], [Task Name]"

   closeRS
   OpenDB

   rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic

  If rs.RecordCount > 0 Then
   Do While Not rs.EOF
     Range("A1").CopyFromRecordset rs
   Loop
  End If


End Sub

It's not a VBA problem - it's an SQL problem. In the SUM part of your statement you haven't placed brackets.

strSQL = "Select [User Name], 
                 [Task Name], 
                 SUM([Project Hrs]) 
from             [idata$]      
group by         [User Name], 
                 [Task Name]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM