MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 10th, 2002, 03:51 PM   #1
agiammo
Board Regular
 
Join Date: May 2002
Location: Austin, TX
Posts: 68
Default

I'd like to edit an existing macro so that it looks in a range of selected cells for the word NO. If it doesn't find any cells with the word NO within the selected range, then I want it to delete the worksheet. If it does find a NO, then I want it to stop so the user can look at the data. Any ideas?
agiammo is offline   Reply With Quote
Old May 10th, 2002, 04:19 PM   #2
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Here it is:

Code:
Sub alrightyThen()
Dim n As Range
Set n = Selection.Find(what:="NO", LookAt:=xlWhole)
If n Is Nothing Then
Application.DisplayAlerts = False
On Error GoTo 1:
ActiveSheet.Delete
Application.DisplayAlerts = True
End If
End
1: MsgBox ("You must have at least one visible worksheet in a standard .xls file")
End Sub
_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-10 15:22 ]
NateO is offline   Reply With Quote
Old May 10th, 2002, 04:22 PM   #3
bergy
Board Regular
 
Join Date: Apr 2002
Posts: 112
Default

Quote:
On 2002-05-10 14:51, agiammo wrote:
I'd like to edit an existing macro so that it looks in a range of selected cells for the word NO. If it doesn't find any cells with the word NO within the selected range, then I want it to delete the worksheet. If it does find a NO, then I want it to stop so the user can look at the data. Any ideas?
this isn't pretty, but I think it works:

Sub test()
ws = 1
wsCount = ActiveWorkbook.Sheets.Count - 1
While ws <= (wsCount)
Sheets(ws).Select
With Worksheets(ws).Range("a1:a500") 'Adjust for your range
Set c = .Find("NO", LookIn:=xlValues)
If c Is Nothing Then
Application.DisplayAlerts = False
Worksheets(ws).Delete
Application.DisplayAlerts = True
'ws = ws - 1
wsCount = wsCount - 1
Else: ws = ws + 1
End If
End With
Wend
End Sub

You will have to adjust for your range. I also have it look for one less sheet than you have in your workbook because it will crash if it tries to delete every sheet. I'm sure this could be written better and more efficiently.
bergy is offline   Reply With Quote
Old May 10th, 2002, 04:23 PM   #4
bergy
Board Regular
 
Join Date: Apr 2002
Posts: 112
Default

See, not only am I slow, but NateO's is much nicer code.
bergy is offline   Reply With Quote
Old May 10th, 2002, 04:33 PM   #5
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Bergy, you were right on target, your code did a little more than mine, looping through each sheet. Have a great weekend all.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 14th, 2002, 08:54 AM   #6
agiammo
Board Regular
 
Join Date: May 2002
Location: Austin, TX
Posts: 68
Default

Hello,

I tried using Nate's code (I didn't need to look on each tab, just the active tab) but it doesn't seem to work as an IF, THEN statement. Even when I plug in a 'NO' the macro still deletes the sheet as if nothing were wrong. And it always gives me the message box too. Any ideas on how to firm this up?
agiammo is offline   Reply With Quote
Old May 14th, 2002, 09:04 AM   #7
agiammo
Board Regular
 
Join Date: May 2002
Location: Austin, TX
Posts: 68
Default

I think I know where the problem is, but my 'fixes' aren't working. here is the code:

'Dim n As Range
Set n = Selection.Find(what:="NO", LookAt:=xlWhole)
If n Is Nothing Then
Application.DisplayAlerts = False
On Error GoTo 1:
ActiveSheet.Delete
Application.DisplayAlerts = True
End If

Now, the selected range of cells has a formula in it that returns either YES or NO. Do I need to specify that it search for a value? I tried changing this line:

If n Is Nothing Then
to this:
If n Is "YES" Then

and it didn't work...got some compile error.
agiammo is offline   Reply With Quote
Old May 14th, 2002, 09:13 AM   #8
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

You can't have the apostrophe in front of your Dim statement. I got the following to work on both xl2000 and xl97. I added an upper lower case so that the macro is not too finicky, if this doesn't help, please post back.

Code:
Sub alrightyThen()
Dim n As Range
Set n = Selection.Find(what:="NO", LookAt:=xlWhole, MatchCase:=False)
If n Is Nothing Then
Application.DisplayAlerts = False
On Error GoTo 1:
ActiveSheet.Delete
Application.DisplayAlerts = True
End If
End
1: MsgBox ("You must have at least one visible " _
 & "worksheet in a standard .xls file")
End Sub
This is testing the area you have selected, vs. a predefined range, is this what you want?

But it does indeed work as an if statement:

It looks at each range in the selection and thinks if there are zero cells in this selection, than this worksheet is history, otherwise, it stays.

Hope this helps.
_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-14 08:57 ]
NateO is offline   Reply With Quote
Old May 14th, 2002, 09:18 AM   #9
agiammo
Board Regular
 
Join Date: May 2002
Location: Austin, TX
Posts: 68
Default

Hey Nate,

I use this function several times in the macro, so I had 'noted' the definition line...sorry for the confusion.

I do want to test only the selected range. It changes from week to week, so I cannot pre-define it.

Let me test this and I'll post back!
agiammo is offline   Reply With Quote
Old May 14th, 2002, 09:42 AM   #10
agiammo
Board Regular
 
Join Date: May 2002
Location: Austin, TX
Posts: 68
Default

Hey Nate,

Ok, this is still not working. you said the code is looking in the selection, and if there are zero cells in the selection, then it deletes? That doesn't make sense to me. I have several hundred rows that are selected. Each cell has a formula that compares one cell against another. If they match, then the formula returns a YES value. If the cells do not match, then I see NO. If all the cells are YES, then I can delete the active sheet and move on. If I get a NO, then I need to stop or pause the macro so that I can look at the data and make corrections. Currently, the macro is deleting the active sheet in both cases.
agiammo is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes