Excel VBA聚合函数错误

时间:2015-04-20 08:58:47

标签: vba excel-vba excel

我是Excel VBA的新手。当我运行下面的代码时,我得到一个错误“在查询表达式sum [Project Hrs]中缺少运算符”。我做错了什么?

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

1 个答案:

答案 0 :(得分:4)

这不是VBA问题 - 它是一个SQL问题。在陈述的SUM部分,您没有放置括号。

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