Filter and Create a New File

luckee

New Member
Joined
Sep 23, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the below updated with the help of another user. After pressing the button on Sheet2, Sheet1 filters and deletes rows based on the conditions. I want it to create a new separate file saved in the same folder with the same button. So summary the output after pressing button on Sheet2 would be:

-Sheet1 will filter and delete rows
-Separate file saved in the same folder with the filtered Sheet1
-Popup message to say "New File Created"

Sub RowFilter()
Dim N As Long, i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1")

N = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row

For i = N To 1 Step -1
If ws.Cells(i, "C") = "IP" Or ws.Cells(i, "H") = "0" And ws.Cells(i, "AK") = "0" Then
ws.Cells(i, "H").EntireRow.Delete
End If
Next i

End Sub

Thank you.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Give this a try:

VBA Code:
Sub RowFilter()
Dim N As Long, i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1")

N = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row

For i = N To 1 Step -1
    If ws.Cells(i, "C") = "IP" Or ws.Cells(i, "H") = "0" And ws.Cells(i, "AK") = "0" Then
        ws.Cells(i, "H").EntireRow.Delete
    End If
Next i

'Obtains the path of ThisWorkbook
Dim fPath As String, folder As String
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
folder = fso.GetAbsolutePathName(ThisWorkbook.Name)
fPath = fso.GetParentFolderName(folder)

'Declares the new file's name (adjust as needed)
Dim fName As String: fName = "newbook " & Format(Now, "YYYYMMDD HHMM") & ".xlsm"

'Use this option to create a copy and save it without closing original
ThisWorkbook.SaveCopyAs Filename:=fPath & "/" & fName

'Use this option to work with the newly created file
'ThisWorkbook.SaveAs Filename:=fPath & "/" & fName

'Verify file saved correctly and return applicable message
Dim strFileExists As String: strFileExists = Dir(fPath & "/" & fName)
    
    If strFileExists = "" Then
        MsgBox "The file could not be saved.", vbCritical, "Unable to Save"
    Else
        MsgBox "New File Created", vbInformation, "Save Success"
    End If

End Sub
 
Upvote 0
You're very nearly there. Try this:
VBA Code:
Sub RowFilter_V2()
    'Your existing code
    Dim N As Long, i As Long
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    N = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
    
    For i = N To 1 Step -1
        If ws.Cells(i, "C") = "IP" Or ws.Cells(i, "H") = "0" And ws.Cells(i, "AK") = "0" Then
            ws.Cells(i, "H").EntireRow.Delete
        End If
    Next i
    
    'Add this bit
    ws.Copy
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Application.DisplayAlerts = False
    wb.SaveAs ThisWorkbook.Path & "\Your New Filename" & ".xlsm", 52
    Application.DisplayAlerts = True
    MsgBox "New File Created"
    
End Sub
 
Upvote 0
Solution
Not answering the question but just curious. I always struggle with using And and Or without using brackets.
Does your if statement actually do what you are intending ?

Book1
ABCDEFGHIAJAK
1Col CCol HCol AK
2IP00
3IP10
4IP01
5IP11
600
7Not Deleted --->10
801
9Not Deleted --->11
Sheet1
 
Upvote 0
Not answering the question but just curious. I always struggle with using And and Or without using brackets.
Does your if statement actually do what you are intending ?

Book1
ABCDEFGHIAJAK
1Col CCol HCol AK
2IP00
3IP10
4IP01
5IP11
600
7Not Deleted --->10
801
9Not Deleted --->11
Sheet1
My initial thoughts as well Alex. I have assumed the existing logic is giving the OP the result they were after - and just added the file save bit. Not the logic I would have used either (i.e. without the brackets).
 
Upvote 0
You're very nearly there. Try this:
VBA Code:
Sub RowFilter_V2()
    'Your existing code
    Dim N As Long, i As Long
    Dim ws As Worksheet
   
    Set ws = ThisWorkbook.Worksheets("Sheet1")
   
    N = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row
   
    For i = N To 1 Step -1
        If ws.Cells(i, "C") = "IP" Or ws.Cells(i, "H") = "0" And ws.Cells(i, "AK") = "0" Then
            ws.Cells(i, "H").EntireRow.Delete
        End If
    Next i
   
    'Add this bit
    ws.Copy
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    Application.DisplayAlerts = False
    wb.SaveAs ThisWorkbook.Path & "\Your New Filename" & ".xlsm", 52
    Application.DisplayAlerts = True
    MsgBox "New File Created"
   
End Sub
thank worked, thank you!
 
Upvote 0
My initial thoughts as well Alex. I have assumed the existing logic is giving the OP the result they were after - and just added the file save bit. Not the logic I would have used either (i.e. without the brackets).
It looked like it did what I wanted, on the one file I tried it on but I still need to test it on some other ones.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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