文件夹中有一些 excel。每个 excel 都遵循相同的模板。每张表中都有一个“K”列。此列包含一些代码(这些代码可以是不同的或重复的)。我正在尝试对这些值应用自动过滤器和想要将每个不同的代码保存在一个单独的 excel 文件中。(例如,如果一个 excel 文件的代码为 15478、15478 和 17832,那么将有一个具有两个代码 15478 的 excel 文件,并且将有第二个具有 17832 的 excel 文件。)并且我必须对文件夹中存在的每个 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
有没有其他方法可以从数组中获取不同的值以对这些值应用过滤器?我尽力解释我的问题,但如果有任何不清楚的地方请告诉我......! Excel工作表模板