strangejosh
New Member
- Joined
- Jul 30, 2022
- Messages
- 15
- Office Version
- 2019
- Platform
- 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
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
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
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