rs.Open "SELECT [Sheet1$].ID FROM [Sheet1$] WHERE [Sheet1$].ID IS NULL ", cn, adOpenKeyset, adLockReadOnly
Sheet1,即我案例中的工作表名称在我的情况下是可变的,我想重复此查询100张,那么如何动态指定名称?
提前致谢。
答案 0 :(得分:1)
首先,您无法在Excel SQL ADO查询中调用整个工作表,但必须指定范围:SELECT [Sheet1$A1:D50].ID FROM [Sheet1$A1:D50] WHERE [Sheet1$A1:D50].ID IS NULL
是的,因为这是一个字符串语句,你可以在其中连接VBA变量。
firstsheetname = "Sheet1"
strSQL = "SELECT [" & firstsheetname & "$].ID FROM [" _
& firstsheetname & "$] WHERE [" & firstsheetname & "$].ID IS NULL
rs.Open strSQL, cn, adOpenKeyset, adLockReadOnly