473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Select & Delete Sheets in Workbook using Wildcard

Hello,

I have an Excel workbook with ~ 21 sheets in it.
Let's say 7 are named "Red1", "Red2", "Red3", ect....
Another 7 are named for "Green1, 2, 3", ect...
and the last 7 are named "Blue1, 2, 3", ect...

I'm trying to write a very basic code to go though the entire Workbook, select and then delete all sheets with the name containing "Red*" (regardless of the following number). So far I have:

Sub DeleteRed()
Dim ws As Worksheet
Dim x As Integer
x = 1
For Each ws In ActiveWorkbook.Sheets
x = x + 1
For Each ws In ActiveWorkbook.Sheets
If Left(ws.Name, 3) = "Red" Then
ActiveSheet.Delete
End If
Next ws
End Sub

Yes I am a novice, and any help would be appreciated!

Thank you.
Jul 2 '08 #1
2 6394
MikeTheBike
639 Expert 512MB
Hi

On the basis that the 'Colour' (or any other string) is always at the begining of the sheet name, then this will delete any name series you care to create.

Note: this will delete sheets without notification. If that is a problem just delete the line
Application.DisplayAlerts = False
from the code

Also it will not delete the last sheet (it will throw an error if you try and delete every sheet).

The UCase() functions make this not case sensitive, again remove these if you want case sensitivity.

Expand|Select|Wrap|Line Numbers
  1. Sub DeleteSheetColour(ByVal strColour As String)
  2.     Dim ws As Worksheet
  3.     Dim NLen As Integer
  4.  
  5.     NLen = Len(strColour)
  6.     For Each ws In ActiveWorkbook.Sheets
  7.         With ws
  8.             If Left(UCase(.Name), NLen) = UCase(strColour) Then
  9.                 If ThisWorkbook.Sheets.Count > 1 Then
  10.                     Application.DisplayAlerts = False
  11.                     .Delete
  12.                     Application.DisplayAlerts = True
  13.                 End If
  14.             End If
  15.         End With
  16.     Next ws
  17. End Sub
  18.  
  19. Sub TestSheetDelete()
  20.     DeleteSheetColour "Green"
  21. End Sub
HTH

MTB
Jul 4 '08 #2
Thanks.... I appreciate your help!!
Jul 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: kmunderwood | last post by:
I am having trouble excluding select xml out to HTML using xsl I want to ignore some xml and turn others red I can not find the right way to both: 1. Only show the <tag> that want to, and...
2
by: Chris Lane | last post by:
Does anybody know of the best way to determine if a worksheet exists in an Excel workbook using VB.NET?
6
by: ashasprabhu | last post by:
hi, how to delete a file using asp.net with vb.net
1
by: Kat | last post by:
I have a master page, with a reference to a style sheet in the header, as usual: <link href="Styles.css" type="text/css" rel="stylesheet" /> I have a content page, with a label on it: ...
1
by: Zuhaib Hyder | last post by:
how to delete client file using c# or javascript ? file delete prmissions are already given?
1
by: unlutunca | last post by:
How can I add and delete a record using PHP. the data will be entered by the web user.
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
1
by: attraankit | last post by:
hi, i am just a newbie in asp world.please tell me how to open an excel workbook using asp and also please tell me what should be the sequence of code while editing html scripts for including asp...
3
by: sandip sen majumder | last post by:
trigger that can track whether that user has done any select,insert,delete,update statement in oracle
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.