Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selectively delete Excel worksheets from Excel workbooks using vba 1

Status
Not open for further replies.

IngDep

MIS
Feb 7, 2008
89
US
Have approximately 85 workbooks that contains 10 worksheets each that are stored in
30 folders.

Objective: I need to delete all worksheets in the workbooks except three that is dependent on the

folder/subfolder. Then, I need to either automatically save the modified workbook with a name such as
"TheNameOfTheWorkbook_GeorgiaJan2008."

For example, the following location contains a workbook titled "StatementOfOper";
D:\Private\Georgia\2008-01

Therefore, I need to delete all worksheets for any other location except Georgia and save the workbook
in the current location as "D:\Private\Georgia\2008-01\StatementOfOper_GeorgiaJan2008."


Hopefully, this is possible? It would definitely eliminate the manual deletion of the folders which
is quite time intensive.


Currently working with the code below but would greatly appreciate any additional insight
regarding the most efficient method to accomplish the objective and how the code below should be
modified.

Also, any way I can initiate this from another worksheet - sort of like a "Control Sheet" instead of
manually opening each Excel workbook that needs to be modified?

Thanks in advance.




Sub DeleteAll()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
If ws. Name = "GA ONE" Or ws.Name = "GA TWO" Or ws.Name = "GA THREE" Then
Else
ws.Visible = xlSheetVisible
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
 




Hi,
Code:
Sub DeleteAll() 
    Dim ws As  Worksheet, a, i as integer, bDelete as boolean
    Application.DisplayAlerts = False 
'split the path into an array
    a = split(thisworkbook.path,"\")
    For Each ws In  ActiveWorkbook.Worksheets 
        bDelete = true
        for i = 0 to ubound(a)
           if ws.name = a(i) then bDelete = false
        next
        If bDelete Then 
            ws.Delete 
        End If 
    Next ws 
    Application.DisplayAlerts = True 
End Sub



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top