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

I need to be able to select columns in Excel by specifying column numbers in vba
How to update a specific row of a table in Excel using Microsoft Graph?
columns' values based on specific rows in pivot table excel
VBA Adding cells from one sheet to another by cell
Searchng a sheet for a value and returning row & column numbers
How to take a control of specific cell of XL?
Vlookup exact text on a cell text string
HTTP GET responds with empty body, downloads excel document in IE
VBA replace comma with line break in word?
Excel online sharing
Change cell value based off of selection from ListBox
How to remove hidden quotes
Sorting out data by insert and copy and paste special
Excel VB to create monthly table based on user input
Week number from Date format in excel(Monthwise_Week Number)
Is there a way to connect to an external powerpivot data model from an Excel workbook?

Categories

HOME
inno-setup
dropbox-api
magento-1.9
encryption
selenium-chromedriver
migration
drools
text
microsoft-cognitive
logistic-regression
freeswitch
speech-recognition
import
contiki
axis2
google-cloud-endpoints
geogebra
autofixture
rpres
codeception
spark-dataframe
windows-authentication
delaunay
strongloop
sourceforge
fontforge
ascii
navbar
webassembly
emu8086
twitter-typeahead
web-component
accurev
recurly
google-nativeclient
probability-theory
greedy
code-injection
osx-elcapitan
ext4
polymer-starter-kit
graphic
week-number
white
pdb-files
gtm-database
puma
product-key
codeigniter-2
leanft
sqlcmd
james
google-nearby
enzyme
lift
fpu
azure-sql-server
ansible-vault
webos
qnx
ifttt
lpc
behavior
matlab-guide
opensmtpd
knockout-validation
sift
android-7.1-nougat
fixed
bubble-sort
use-case
sonarqube-4.5
dde
agile-project-management
client-side
rouge
grape
nscoding
mime-mail
arrangeoverride
inventory
shapely
network-monitoring
execution-time
argo
euclidean-distance
du
jaxb2-maven-plugin
setlocale
kirby
ncbi
zooming
mooc
ember.js-view
member
nools
kooboo
android-handlerthread
psychology
intel-galileo
resharper-7.1
gideros
meteor-slides
tandem
eclipse-wtp
uipicker
ruby-on-rails-plugins
jquery-mobile-collapsible
hyperterminal
static-array
memory-mapped-files
nsinteger
launching-application
abpeoplepickerview
codeigniter-datamapper
expandoobject
pasteboard
juggernaut
set-include-path
viewexpiredexception
dynamic-css
loose-coupling
white-box-testing
google-notebook

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile