question

SamWhite-6017 avatar image
0 Votes"
SamWhite-6017 asked SamWhite-6017 edited

Filter excel file based on multiple criteria and copy filtered contents to another sheet

I have multiple excel files, I want to filter a column name ( Items) using multiple criteria and paste the contents into another excel file. Sample source excel file attached.


198062-capture12.png

In this example, I want to filter using criteria "Rice", "Vegetables" and "Butter" and copy to another sheet.
I have come up with the below code, it completes however it does not copy anything.
=================================================================

================================================

 $column = 2 
 $sfile = "C:\Temp\FileA.xlsx"
 $criteria = "Rice","Vegetables","Butter"
 $xl = New-Object -ComObject Excel.Application
 $xl.Visible = $false
 $swb = $xl.Workbooks.Open("$sfile")
 $swb = $swb.Worksheets.Item(1)
 $srange = $swb.UsedRange
 $srange.EntireColumn.AutoFilter()
 $srange.AutoFilter($column, $criteria)


windows-server-powershelloffice-scripts-excel-dev
capture12.png (8.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

My advice: don't use the Excel.Application COM object (or any other COM object, but especially anything related to MS Office) if you can. It's unlikely you'll find MS Office installed on a server, and many office apps are security problems waiting to happen.

Instead, install the ImportExcel module (Install-Module ImportExcel -Scope <your-choice-here>) and then do the work using PowerShell:

 $criteria = "Rice","Vegetables","Butter"
 $column = 'Items'
 Import-Excel -Path c:\Junk\sourcecopy.xlsx |        # imports 1st worksheet by default
     ForEach-Object{
         if ($criteria -contains $_.$column){
             $_
         }
     } | Export-Excel -Path c:\junk\File1.xlsx


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SamWhite-6017 avatar image
0 Votes"
SamWhite-6017 answered RichMatheisen-8856 commented

Please suggest how to accomplish this using the AutoFilter or AdvancedFilter method. Due to some limitations I cannot use the module you have suggested.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'd look for answers to your problem using visual basic and then convert them to PowerShell. Try a search with "excel visual basic autofilter". I expect you're missing the XlAutoFilterOperator -- but I don't know that much about using Excel to say that authoritatively.

0 Votes 0 ·
SamWhite-6017 avatar image
0 Votes"
SamWhite-6017 answered

Any suggestions on this?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SamWhite-6017 avatar image
0 Votes"
SamWhite-6017 answered RichMatheisen-8856 edited

Any example on how to use the advanced filter for this case?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That's more of an Excel question than a PowerShell one. However, wouldn't it look something like this:

 .AutoFilter( 2, Criteria1:=Array("Rice","Vegetables","Butter"), Operator:=xlFilterValues)
0 Votes 0 ·
SamWhite-6017 avatar image SamWhite-6017 RichMatheisen-8856 ·

I meant to ask how we can use Autofilter for excel from within powershell for filtering on three or more conditions.
The example you gave is for vba and does not work in powershell.

0 Votes 0 ·

I didn't say it would work in PowerShell, I asked if it should look like that. Note that there are three arguments, not just the two in your code.

 .AutoFilter( 2, $criteria, 7)

excel.range.autofilter
excel.xlautofilteroperator


0 Votes 0 ·
SamWhite-6017 avatar image
0 Votes"
SamWhite-6017 answered

Any suggestions on this?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.