Solved

Excel VBA- Delete WS based on DV List Value

Posted on 2013-01-24
3
414 Views
Last Modified: 2013-01-24
Hi Experts,  :)

I'm creating a workbook that auto adds worksheets and name them based on values within range A4:A23.  The user would click on the yellow image to have named sheet(s) created.  

Name Range Sample
I need VBA that'll delete worksheets whose name is in the DV list in cell A2.

Sample DV List
Is this possible?  Sample workbook is attached also

Thank you!
SampleDeleteWSBasedCriteria.xlsm
0
Comment
Question by:"Abys" Wallace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Shanan212 earned 500 total points
ID: 38815655
See code/attached worksheet.

Try clicking the button next to A2 to run code.

Sub deletesheet()
    
    Dim e As String, iRow As Long, ws As Worksheet, main As Worksheet
    
    Set main = ActiveSheet
    e = ActiveSheet.Range("A2").Value
    
    ActiveSheet.AutoFilterMode = False
    Range("A3:F3").Select
    Selection.AutoFilter
    
    iRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
    
    If Application.WorksheetFunction.CountIf(Range("A5:A" & iRow), e) > 0 Then
        
        For Each ws In ActiveWorkbook.Worksheets
            ws.Activate
            If ws.Name = e Then
                Application.DisplayAlerts = False
                Sheets(e).Delete
            End If
        Next ws
        main.Select
        Range("A2").Select
        MsgBox "Deleted " & e, vbInformation
    Else
        MsgBox "Your selection is not found among the list!", vbExclamation
    End If
    
End Sub

Open in new window

SampleDeleteWSBasedCriteria.xlsm
0
 

Author Closing Comment

by:"Abys" Wallace
ID: 38815815
Shanan212:  Thank you!  works perfect!  and I like the msg box that advises which sheet was just deleted.
0
 
LVL 13

Expert Comment

by:Shanan212
ID: 38815842
You are welcome :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fix this UDF to handle blank cells? 8 46
NEED LOOK FOR NUM 23 29
Excel + CountIfs + two colums 5 39
excel file icon - over last few months weird 2 26
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question