使用vbscript中的不同值在特定列上应用自动筛选

时间:2013-02-22 17:55:45

标签: excel vbscript autofilter

文件夹中有一些excel。每个excel都遵循相同的模板。每个工作表中都有一个“K”列。此列包含一些代码(这些代码可以是不同的或重复的。)我正在尝试应用autofilter这些值并希望将每个不同的代码保存在一个单独的excel文件中。(如果一个excel文件有代码15478,15478和17832,那么将有一个excel文件同时具有代码15478,并且将有第二个excel文件具有17832 。)这就是我必须为文件夹中的每个excel文件做的。

strPath="C:\Testr"
Set objExcel= CreateObject("Excel.Application")
objExcel.Visible= True

Const xlUp= -4162

'To store every excel sheet column values in array
Dim a(), val

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder (strPath)

'To pick out the only distinct values from array
 Set objDict= CreateObject("Scripting.Dictionary")
 objDict.CompareMode = vbTextCompare

'Searching for xls files
For Each objFile In objFolder.Files
If objFso.GetExtensionName(objFile.Path) = "xls" Then
    objExcel.Workbooks.Open(objFile.Path)


    'Counting all the used rows
    With objExcel.Activeworkbook.Sheets("PAAF")
    rowcount=.Range("A" & .Rows.Count).End(xlUp).Row-2

    msgbox rowcount
    End With

    'dynamic array
    Redim Preserve a(rowcount)

    'Collecting distinct values in dictionary object
    for i=7 to rowcount
        a(i-7) = objExcel.Activeworkbook.Sheets("PAAF").Cells(i,11).Value
    objDict(a(i-7)) = a(i-7)

    Next


    n = objDict.Items

    REM The problem is here, though i am able to get unique values,
     REM but these values are getting processed for every excel because 
     REM the loop is starting for 0 .Ex- if one sheet is having code 19873,
     REM it will get processed for second sheet also whether it is present that 
     REM or not.

    For i=0 to ubound(n)
    msgbox n(i)




With objExcel.Activeworkbook.Sheets("PAAF")

    'Passing dictionary stored values to apply autofilter on    
    .Range("K6").AutoFilter 11,"="& n(i)

End With

fn=objFso.GetFileName(objFile.Path)
objExcel.Activeworkbook.saveAs("C:\Test2\"&"Output"& fn &i &".xls")

Next    

有没有其他方法可以从数组中获取不同的值以对这些值应用过滤器?我尽力解释我的问题,但如果有任何不清楚的话,请告诉我....! Template of Excel sheet

0 个答案:

没有答案