VBA script: How to filter on 2 criteria, paste to new worksheet and email? unsolved

strangejosh

New Member
Joined
Jul 30, 2022
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
Hello!!

I figured out part of the VBA script but I am stuck. I have only been able to figure out part of it.

I can filter on unique vendor number in column B (2) and send each of those results to a new worksheet but I also need to filter column V (22) on whatever the value in Sheet2 cell C3
Annotation 2022-07-30 121801.jpg
is.

And bonus points if someone can help me figure out how to email each those new worksheets to the corresponding vendor?

To: value in each new worksheet is located in U2

CC: value in W2

Subject: Past Due PO's

Body: Hello "Value in cell T2",

Please see attached past due orders. Please provide status update.

Thank you.

Below is the script I have so far and again I can't figure out how to filter on 2 critera.

Option Explicit

Sub filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim rng1 As Range
Dim last As Long
Dim sht As String
Dim newBook As Excel.Workbook
Dim Workbk As Excel.Workbook

'Specify sheet name in which the data is stored
sht = "Sheet1"

'Workbook where VBA code resides
Set Workbk = ThisWorkbook

'change filter column in the following code
last = Workbk.Sheets(sht).Cells(Rows.Count, "B").End(xlUp).Row

With Workbk.Sheets(sht)
Set rng = .Range("A1:X" & last)
End With

Workbk.Sheets(sht).Range("B1:B" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True

' Loop through unique values in column
For Each x In Workbk.Sheets(sht).Range([AA2], Cells(Rows.Count, "AA").End(xlUp))

With rng
.AutoFilter
.AutoFilter Field:=2, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy

'Add New Workbook in loop
Set newBook = Workbooks.Add(xlWBATWorksheet)

newBook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
newBook.Activate
ActiveSheet.Paste
End With

'Save new workbook
newBook.SaveAs x.Value & ".xlsx"

Next x

' Turn off filter
Workbk.Sheets(sht).AutoFilterMode = False

With Application
.CutCopyMode = False
.ScreenUpdating = True
End With

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Add another filter line.

VBA Code:
With rng
.AutoFilter
.AutoFilter Field:=2, Criteria1:=x.Value
.AutoFilter Field:=22, Criteria1:=sheets("Sheet2").range("C3").value
.SpecialCells(xlCellTypeVisible).Copy

You should also be able to loop through unique values in column B, instead of creating a unique list.

Not sure why you would add a new workbook, then add a sheet to that workbook.
 
Upvote 0
So that did work but for the first record and then it errored out.

Subscript out of range.

Debug

.AutoFilter Field:=22, Criteria1:=Sheets("Sheet2").Range("C3").Value

Not sure why it worked for the first record and then stopped?
Add another filter line.

VBA Code:
With rng
.AutoFilter
.AutoFilter Field:=2, Criteria1:=x.Value
.AutoFilter Field:=22, Criteria1:=sheets("Sheet2").range("C3").value
.SpecialCells(xlCellTypeVisible).Copy

You should also be able to loop through unique values in column B, instead of creating a unique list.

Not sure why you would add a new workbook, then add a sheet to that workbook.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top