VBA for Deleting Sheets & Save As Based on Column Values

krisoey

New Member
Joined
Aug 7, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I need help with VBA for deleting sheets that do not have certain characters (based on cell values in a column A) and do not equal to sheet name "IWF Extract", and then saving them based on those cell values as well.

Hoping that i can loop through all of the cell values within the column A

Thank you in advance

1705955374596.png
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
To be clear:
Delete any sheet that does not have your table values AND do not = IWF EXTRACT

What are you saving the workbook?
 
Upvote 0
Correct on deleting any sheet that does not the table values (and hoping that they can be deleted in a loop based on those values) and do not = IWF EXTRACT. So, that means whatever that has 5141 and = IWF Extract is retained

On the second question, I'm hoping to save the workbook based on those values in the table as well. So, the workbook will be saved as 5141.xlsm; 5142.xlsm, etc.

In the end, the workbook 5141.xlsm will only have sheets that have 5141 and IWF Extract, 5142.xlsm will only have sheets that have 5142 and IWF Extract, etc

Thank you :)🙏
 
Upvote 0
I think this is what you are asking. Will there ever be tabs with multiple 5141?
Maybe populate this table a bit more so we are doing it correct

MasterWorkbook.xlsm
BCD
4Current Sheet NameSheet Name in New workbook
5IWF EXTRACTKeepIWF EXTRACT
6IWF (COPY PASTE)Delete
7DIRECTORY PARSEDelete
8DIRECTORYDelete
9SPHEREx 5141Keep5141
10TEX 5141???
11
5146
 
Upvote 0
Ok first make sure to make a copy of your workbook.

I am not deleting anything at this point. Using what I think the logic is above. I created a brand new workbook in a directory with the sheet IWF EXTRACT and if exists the production Id from the table.

Let me know if this is what you were thinking


VBA Code:
Option Explicit

' make sure to make a backup of your master sheet before running this
' i would also put a bunch of error checking in this

Public Sub DeleteAndCopyProductionSheets()

    Const prodDirectory As String = "C:\ProductionWorkbooks\"  ' where you want the new workbooks to go
    
    Dim wb As Workbook, newWorkbook As Workbook
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim wbName As String
    Dim i As Integer, x As Integer, iwfIndex As Integer
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    
    ' get all your production names from the table
    Set tbl = shMaster.ListObjects("tblProduction") ' this is your table of numbers, production units change prior running
    Set wb = ThisWorkbook
    
    For i = 1 To tbl.ListRows.Count
        wbName = tbl.ListRows(i).Range(, 1).Value

        ' delete the existing workbooks
        If Dir(prodDirectory & wbName) <> "" Then
            Kill prodDirectory & wbName
        End If

        ' find IWF EXTRACT
        iwfIndex = ReturnWSheetIndexByName(wb, "IWF EXTRACT")

        ' create a new, blank workbook
        Set newWorkbook = Workbooks.Add
        
        If iwfIndex > 0 Then
            wb.Activate
            wb.Worksheets(iwfIndex).Copy Before:=newWorkbook.Sheets(1)
            newWorkbook.Sheets("Sheet1").Delete
        End If
        
        ' now try to find the worksheet with the wbName...Note this will match the first tab with the name in the sheet name
        iwfIndex = ReturnWSheetIndexByInstr(wb, wbName)
        If iwfIndex > 0 Then
            wb.Activate
            wb.Worksheets(iwfIndex).Copy After:=newWorkbook.Sheets(1)
        End If

        ' save the new workbook with the specified name
        newWorkbook.SaveAs prodDirectory & wbName, FileFormat:=52
        newWorkbook.Close SaveChanges:=True
    Next
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    
    Set wb = Nothing
    Set ws = Nothing
    Set tbl = Nothing
End Sub

Private Function ReturnWSheetIndexByName(wb As Workbook, sheetName As String) As Integer
    Dim i As Integer
    
    wb.Activate
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = sheetName Then
            ReturnWSheetIndexByName = i
            Exit Function
        End If
    Next
    
    ReturnWSheetIndexByName = -1
End Function

Private Function ReturnWSheetIndexByInstr(wb As Workbook, sheetName As String) As Integer
    Dim i As Integer
    
    wb.Activate
    For i = 1 To Worksheets.Count
        ' Check if sheet name contains the specified substring
        If InStr(1, Worksheets(i).Name, sheetName, vbTextCompare) > 0 Then
            ReturnWSheetIndexByInstr = i
            Exit Function
        End If
    Next
    
    ReturnWSheetIndexByInstr = -1
End Function
 
Upvote 0
I think this is what you are asking. Will there ever be tabs with multiple 5141?
Maybe populate this table a bit more so we are doing it correct

MasterWorkbook.xlsm
BCD
4Current Sheet NameSheet Name in New workbook
5IWF EXTRACTKeepIWF EXTRACT
6IWF (COPY PASTE)Delete
7DIRECTORY PARSEDelete
8DIRECTORYDelete
9SPHEREx 5141Keep5141
10TEX 5141???
11
5146
Good morning,

Yes there will be multiple tabs for 5141 (and 5142, 5143, 5144, etc)

Backstory is that i will be creating various tabs for 5141 (e.g. project1 - 5141, project2 - 5141, etc), 5142 (e.g. project1 - 5142, project2 - 5142, etc), 5143 (e.g. project1 - 5143, project2 - 5143, etc), up to 5146 (and might go up to 514x depending on the needs). Now I need to separate and distribute these tabs based on the 514x numbers (including the IWF EXTRACT).

I will try the code that you provided :)

Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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