excel


Delete worksheet if name isn't found in a cell in column D


I asked a question earlier about a worksheet_change macro I was working on. It's almost complete, but now I'm stumped. I'm trying to loop through all the worksheets in the workbook, and if the worksheet's name is not found in any cell in range D6:D34, I want to delete the worksheet. How can I write this? I'm completely stumped. Current code:
Private Sub WorkSheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False 'Run faster
Application.DisplayAlerts = False 'Just in case
'To add worksheets automatically
Dim shtName As Variant
For Each shtName In Sheets(1).Range("D6:D34")
If shtName <> "" Then
If WorksheetExists((shtName)) Then 'do nothing
Else
ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = shtName
Sheets("Admin").Select
End If
Else 'there's no sheet
End If
Next
'to delete sheets with no matching value
Dim ws_count As Integer
Dim i As Long
ws_count = ActiveWorkbook.Worksheets.Count
For i = 1 To ws_count
'what do I need here???
Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Function WorksheetExists(sName As String) As Boolean
WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function
Dim ws As Worksheet
ws_count = ActiveWorkbook.Worksheets.Count
For i = ws_count To 2 Step -1
Set ws = ActiveWorkbook.Worksheets(i)
If IsError(Application.Match(ws.Name,Sheets(1).Range("D6:D34"),0)) then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next i

Related Links

With the code below I receive the error Variable not defined
Run-Time Error 462 - Delete Outlook Appointments from Excel
VBA - thisworkbook.path
VBA code running but not having effect
VBA-style range selection in Excel
Sorting Excel data
MS Office - ActiveX Buttons switching places
Not able to access excel file
Sending Excel VBA email
Excel VBA 'If Then Else' Statement fails
How to run sub procedure [closed]
How to compare data where data matches (or doesn't match) on two columns?
Left vlookup error
GAS - Script to duplicate a script
Convert Excel VBA Analysis ToolPack “Fourier” Sub into a UDF Function
Excel VBA look through sheets and copy column range to another sheet

Categories

HOME
cordova
objective-c
google-maps
processing
cron
rx-java
alexa
encryption
function
soap
webserver
mapkit
wayland
opentsdb
aurelia-router
google-cardboard
strongloop
fontforge
bit-manipulation
ssrs-2016
floyd-warshall
eureka-forms
graphic
pi4j
drools-fusion
openfiledialog
reactive-streams
sequences
state
lektor
smtpclient
xfs
homescreen
3d-modelling
notnull
uievent
caliburn.micro
multibox
libphonenumber
autocorrect
evosuite
equation-solving
oltp
erlang-shell
sceditor
collaborative-filtering
c#-to-vb.net
express-stormpath
vibration
devpartner
graphic-effects
customdialog
jquery-widgets
frontpage
mindbody
opcode
jseparator
gridsplitter
angular-ui-tree
mpi4py
window-position
unexpectendoffile
bpopup
qmediaplayer
cordova-chrome-app
wave
google-text-to-speech
zooming
magnetometer
review
zenoss
vin
function-prototypes
sqloledb
deface
restkit-0.20
zurb-joyride
sine-wave
nsurlprotocol
google-email-settings-api
citrix-access-gateway
mindmap
struts2-tiles-plugin
jvm-crash
jpreloader
operational-transform
hyperterminal
django-sessions
pubchem
glossary
texinfo
assetslibrary
test-environments
alasset
dynamic-css
sqlmetal
pointer-arithmetic
o3d

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile