簡體   English   中英

使用數組值在列上應用自動過濾器

[英]Apply autofilter on column using arrays value

我正在檢索特定列的所有值,並將所有值存儲在數組中,以便可以使用這些值來應用自動過濾器。

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

VBScript不會解析字符串中的變量。 當將a(i-2)放在雙引號中時,它將變成文字字符串"a(i-2)"而不是數組a的字段i-2的值。

.AutoFilter 2, "=a(i-2)"更改為.AutoFilter 2, "=" & a(i-2) ,問題將消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM