我在VBA中创建了一个循环,该循环应该更改代码strSql部分中的变量(j)。我在excel工作表上的输出列都是缩进的。这意味着在strsql字符串中不考虑变化的变量j。有人知道在这种情况下如何使用适当的变量吗?提前谢谢
Set 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

最佳答案

你得把j从你的绳子上移开
登记日期)到
注册日期)

07-28 00:45