473,322 Members | 1,307 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,322 software developers and data experts.

Delete Worksheets in Excel Workbook with a Condition?

I have a workbook with many sheets, and I am trying to delete the sheets that have a certain value in cell A1. My code will not continue going to the next sheets once the condition is not met. Can anybody help with this code?

Dim I As Integer

For I = 1 To Sheets.Count
ActiveSheet.Range("A1").Select
If ActiveCell = "BUDGET" Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True

End If
Next
Aug 31 '07 #1
5 3358
kadghar
1,295 Expert 1GB
I have a workbook with many sheets, and I am trying to delete the sheets that have a certain value in cell A1. My code will not continue going to the next sheets once the condition is not met. Can anybody help with this code?

Dim I As Integer

For I = 1 To Sheets.Count
ActiveSheet.Range("A1").Select
If ActiveCell = "BUDGET" Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True

End If
Next
this is because once you delete Worksheets(1) the one that used to be Worksheets(2) turns into worksheets(1)

i recomend you to use a DO that only increases the counter when you dont delete the sheet, something like

Expand|Select|Wrap|Line Numbers
  1. i =1
  2. application.displayalerts = false
  3. do
  4.     if worksheets(i).cells(1,1).value = "BUDGET" then
  5.         worsheets(i).delete
  6.     else
  7.         i = i +1
  8.         if i > worksheets.count then exit do
  9.      end if
  10. loop
  11. application.displayalerts = true
hope that helps
Aug 31 '07 #2
Tig201
103 100+
I have a workbook with many sheets, and I am trying to delete the sheets that have a certain value in cell A1. My code will not continue going to the next sheets once the condition is not met. Can anybody help with this code?

Dim I As Integer

For I = 1 To Sheets.Count
ActiveSheet.Range("A1").Select
If ActiveCell = "BUDGET" Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True

End If
Next
You could also reverse your Loop to work from the back of the workbook to the front.
Aug 31 '07 #3
kadghar
1,295 Expert 1GB
You could also reverse your Loop to work from the back of the workbook to the front.
yeap, that's an easier way :)
Aug 31 '07 #4
SammyB
807 Expert 512MB
Even easier is a for each loop. Also, you want to use the Worksheets collection instead of the Sheets collection. A Sheet object can be a Chart sheet and looking at A1 will crash and burn!

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Sub Macro1()
  3.     Dim ws As Worksheet
  4.     Application.DisplayAlerts = False
  5.     For Each ws In Worksheets
  6.         If ws.Cells(1, 1) = "Budget" Then _
  7.             ws.Delete
  8.     Next ws
  9.     Application.DisplayAlerts = True
  10. End Sub
  11.  
Aug 31 '07 #5
Great. Thank you very much for the help.
Sep 4 '07 #6

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

Similar topics

5
by: Iris | last post by:
I have 8 text files (tab delimited) that I would like to import into an Excel workbook as 8 individual worksheets but I cannot find any example code on this subject. Can anyone help me please???? ...
0
by: dleh | last post by:
I have been tasked with producing a server application that will convert excel files to html. I have no experience of developing under Windows, (but plenty of UNIX/C++ experience) but I picked...
4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
1
by: J Daniel Melton | last post by:
Hello, I am using late binding in a managed VC++ .NET 2003 application. I used KB 302902 (for C#) as a starting point and converted it to managed C++. I built a managed class that is intantiated...
4
by: e.h.doxtator | last post by:
All I'm a Python newbie, and I'm just getting to the wonders of COM programming. I am trying to programmatically do the following: 1. Activate Excel 2. Add a Workbook 3. Add a Worksheet...
3
by: sun919 via DotNetMonster.com | last post by:
hi , i have a question to ask regarding deleting the worksheet basically i have written code which find the select worksheet which work fine but it didn't delete the worksheet from the workbook...
3
by: mike11d11 | last post by:
I was able to create three worksheets in my workbook, but when I go to add the 4th I get an Invalid Index error. I must be leaving something out to when adding 4 or more sheets. Thanks Dim...
2
by: Bxitty | last post by:
Hello all, I am trying the below code to delete a worksheet using VB.NET..I amgeeting an error with the delete...any idea where this could be wrong.. Dim str_path As String Dim...
3
by: Will | last post by:
Can someone help with code to delete multiple columns from an excel spreadsheet? I know which columns I need to delete. The code below will delete a single column but I'm not sure how to delete...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.