1

我正在检索特定列的所有值并将所有值存储在一个数组中,以便我可以使用这些值来应用自动过滤器。

rowcount=objExcel.Activeworkbook.Sheets(1).UsedRange.Rows.count

Dim a()
Redim Preserve a(rowcount)
'msgbox rowcount

'Storing all column values in an array'

for i=2 to rowcount
  a(i-2) = objSrcWorksheet.Cells(i,7).Value
Next

'Checking values of array'
for i=2 to rowcount
  msgbox a(i-2)

'Applying Autofilter'
 With objSrcWorksheet


.Range("G1").AutoFilter 2,"=a(i-2)"  'Problem is here'
'.Range("G1").AutoFilter 2,"=2"     'While hardcoded values are working'

 End With
Next
4

1 回答 1

1

VBScript 不解析字符串中的变量。当您放入a(i-2)双引号时,它会变成文字字符串,而不是array"a(i-2)"的字段值。i-2a

换成.AutoFilter 2, "=a(i-2)".AutoFilter 2, "=" & a(i-2)问题就会消失。

于 2013-02-17T15:10:49.820 回答