我在VBA中创建了一个循环,它应该改变代码的strSql部分中的变量(j)。 excel表上的输出列都是缩进的。这意味着在strsql字符串中不考虑更改变量j。在这种情况下,有人知道如何使用适当的变化变量吗?提前致谢
设置rs =新ADODB.Recordset
For j = 1 To 6
strSql = "SELECT COUNT(*) FROM( " & _
"SELECT COUNT(*) as nbp FROM `order` " & _
"JOIN user ON user.id = order.destination_id " & _
"AND DATEDIFF(date_added , register_date) <= j" & _
"GROUP BY destination_id) c " & _
"GROUP BY nbp " & _
"ORDER BY c.nbp ASC ; "
rs.Open strSql, oConn, adOpenDynamic, adLockPessimistic
res = rs.GetRows
rs.Close
For i = 1 To 2
Cells(i, j) = res(0, i - 1)
Next i
Next j
答案 0 :(得分:4)
你需要将j移出你的字符串
register_date)&lt; = j“&amp;
要
register_date)&lt; =“&amp; j&amp;
答案 1 :(得分:2)
相当令人讨厌,但在vba与ado我猜这是最快的黑客...你不需要将j转换为字符串但我宁愿明确......
strSql = "SELECT COUNT(*) FROM( " & _
"SELECT COUNT(*) as nbp FROM `order` " & _
"JOIN user ON user.id = order.destination_id " & _
"AND DATEDIFF(date_added , register_date) <= " & _
CStr(j) & " " & _
"GROUP BY destination_id) c " & _
"GROUP BY nbp " & _
"ORDER BY c.nbp ASC ; "