Solved

Excel VBA- Delete WS based on DV List Value

Posted on 2013-01-24
3
415 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

626 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