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

How to add contents in column aside of a column in excel?
Find value from columns
VBA - 1004 Error when using Advanced Filter
Run-Time error '2147417848 (80010108)': Method 'Value' of object 'Range' Failed
Excel Conditional Formatting Based on Team Name
Visual Basic ParamArray args: The formula contains unrecognized text
<Excel-VBA> “Type mismatch” error while copying value from cell into textbox
Loop through a row until blank
Excel Mac 2016 vba fill.Userpicture from range
Vlookup - return multiple values / array in a macro
making end of date shorter in Gantt - Excel
Build 1D array / list in formula by multiplying values for use in AVERAGE()
Error adding numeric to text
Dynamic referencing the UsedRange in VBA
VBA formula does not work properly
Copy&Paste Data Multiple Criteria Excel

Categories

HOME
reporting-services
multidimensional-array
set
bloomberg
scenekit
logistic-regression
keen-io
zurb-foundation
visual-studio-mac
q-learning
ssd
geogebra
amazon-cloudsearch
playframework-2.5
openmeetings
bigdata
flannel
android-toast
apigee
dijkstra
business-objects
sensenet
prestashop-1.6
laravel-blade
4d-database
nlog
stackdriver
raycasting
apache-felix
postmates
mikrotik
bug-tracking
indexof
autoload
atom-feed
cad
bluehost
cloudfoundry-uaa
gmail-contextual-gadgets
gsutil
fixtures
crossfilter
nsubstitute
protactor
fluent-nhibernate
background-position
getelementsbyclassname
evosuite
tinyxml2
equation-solving
c++1z
snobol
embedded-sql
spring-social-facebook
sonarqube-4.5
jwrapper
htsql
dex2oat
kendo-datepicker
reducers
firefox-addon-bootstrap
jquery-ui-resizable
tvml
couchbase-sync-gateway
frontpage
stunnel
pysqlite
github-linguist
authentication-flows
typescript1.7
childviewcontroller
wunderlist
ontouchevent
curator
spool
workbench
jml
lcm
google-playground
complete.ly
microsoft-search-server
resharper-7.1
mapstraction
contactscontract
where-in
firepad
hyperterminal
alpha-five
generic-list
osx-gatekeeper
mbprogresshud
scalaquery
kiln
windows-explorer
smalldatetime
multiple-versions
id-generation
maintenance-plan

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile