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 14th, 2002, 01:16 PM   #1
Lisa
Board Regular
 
Join Date: Apr 2002
Location: Indianapolis
Posts: 80
Default

I've recorded code to sub-total data in a worksheet (the sub-total function under data). Now what I'd like to do is eliminate any lines which sub-totals to zero if a certain column meets criteria.

Example: Subtotal on User field, sum on calc field. Then, if MatchFlag field = "Matched" and sub-total = 0, then delete all rows in that subtotal.

Is this possible? I'm at a loss on how to loop through the sub-totals(and how to refer to the sub-totals) and then eliminate the rows that meet the criteria.

Thanks for any help

Lisa
Lisa is offline   Reply With Quote
Old May 14th, 2002, 01:52 PM   #2
cornbread
Board Regular
 
Join Date: Apr 2002
Location: Oregon
Posts: 77
Default

How is your data arranged? How many subtotals are there? are they always going to be in the same cell? Will each subtotal cell always have a value in it? Do you care if the code clears the subtotal value if it is equal to zero? are your subtotals always the same number of cells or rows away from each other? If you give a little more info I might already have some code that does what you are looking for. If not I'm sure myself or some one else here can cook you some up real quick. I just need more parameters on what it should do.
cornbread is offline   Reply With Quote
Old May 14th, 2002, 04:46 PM   #3
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Here's a starting point. This loops through all of your formulae in the activesheet looking for 'SUBTOTAL.' If it's equal to 0, and your key field is enabled, it grabs the range in the function, and deletes the cells comprising the subtotal:
In this example, Matched is two columns to the right of each subtotal function.


Sub clr()
'Macro recorded by Nate
Dim o As Integer, s As Integer
Dim z As String, cl As Range, frmcl As Range
On Error Resume Next
Set frmcl = Sheets(3).[a1].SpecialCells(xlFormulas)
If frmcl Is Nothing Then GoTo 1
For Each cl In frmcl
frm = cl.Formula
If InStr(frm, "SUBTOTAL") And cl.Value = "0" _
And cl.Offset(, 2).Value = "Matched" Then '2 columns to right
o = InStr(cl.Formula, ",")
s = InStr(cl.Formula, ")")
z = Mid(cl.Formula, o + 1, s - 1 - o)
cl.EntireRow.Delete 'deletes Subtotal Row
Range(z).EntireRow.Delete 'deletes rows in Subtotal Range
End If
Next cl
End
1: MsgBox "Could not find any formulae in the active sheet. Try Again."
End Sub


Caution, Just because a Subtotal = 0 does not mean that all values above equal 0. It may make more sense to loop through the range, which we just grabbed, and look for blanks or zeros and eliminate those. Food for thought. Let us know.


_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-14 16:07 ]
NateO is offline   Reply With Quote
Old May 14th, 2002, 06:38 PM   #4
Lisa
Board Regular
 
Join Date: Apr 2002
Location: Indianapolis
Posts: 80
Default

Thanks for the input Nate and Cornbread. I'm home now and don't have the file with me, but will try it in the morning (midwest US time)

Lisa
Lisa is offline   Reply With Quote
Old May 14th, 2002, 06:46 PM   #5
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Hey Lisa, make sure you read the last part closely, when you say matched field are you talking about a field beside each entry (that is part of the subtotal function)? If so, we'll need to use the extended version I haven't posted yet. Post back.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 15th, 2002, 07:05 AM   #6
cornbread
Board Regular
 
Join Date: Apr 2002
Location: Oregon
Posts: 77
Default

Well it's morning North West time so mabye you have slept in. Nate's sub is almost exactly what I have for clearing a range based on what range is in a formula. I may be out of the office today but I will check this thread again later in the day. Post back
if you need more code than what Nate gave or if you need a different solution.
cornbread is offline   Reply With Quote
Old May 15th, 2002, 08:56 AM   #7
Lisa
Board Regular
 
Join Date: Apr 2002
Location: Indianapolis
Posts: 80
Default

Good morning, Nate and Cornbread...

Some additional info:
1) There won't be any zeros or blanks in the fields I'm subtotalling... I'm attempting to get rid of entries that equal zero, say 3 lines with 25.00, -12.00, -13.00.

2) The "Matched" data is not on the subtotal row, but on the rows the contain the data being subtotalled. So I put a -1 in Nate's code to at least check the last row. In a perfect world, I'd like to verify that all those say "Matched" before eliminating the range.

3) I moved the "frmcl" to AL1 instead of A1, as I have a column header in A1. Is this the right thing to do?

4) Nate, I've spent over an hour trying to decipher the InStr and Mid section... (I really wasn't sleeping in, Cornbread!) Can you tell me what's happening there? I'm trying to learn here, not just solve immediate problems.

5) In my 3300 line file (before the subtotals are put in), approx 1700 lines cleared - that's very good. However, there are probably another 1000 lines that should have cleared. I'll need to do a little more research to see if I can discern a pattern.

6) Here's the code I'm using... pretty much verbatim Nate's code.

Sub clr()
'Macro recorded by Nate
Dim o As Integer, s As Integer
Dim z As String, cl As Range, frmcl As Range
On Error Resume Next
Set frmcl = Sheets("input").[al1].SpecialCells(xlFormulas)
If frmcl Is Nothing Then GoTo 1
For Each cl In frmcl
frm = cl.Formula
If InStr(frm, "SUBTOTAL") And cl.Value = "0" _
And cl.Offset(-1, -16).Value = "Matched" Then '16 columns to left
o = InStr(cl.Formula, ",")
s = InStr(cl.Formula, ")")
z = Mid(cl.Formula, o + 1, s - 1 - o)
cl.EntireRow.Delete 'deletes Subtotal Row
Range(z).EntireRow.Delete 'deletes rows in Subtotal Range
End If
Next cl
End
1: MsgBox "Could not find any formulae in the active sheet. Try Again."
End Sub


Like I said, I'm not sure why it's not working for all the rows which meet the criteria.

Thanks for all your help and education.

Lisa

Lisa is offline   Reply With Quote
Old May 15th, 2002, 09:14 AM   #8
Vas
Board Regular
 
Join Date: May 2002
Location: Gothenburg, Sweden
Posts: 74
Default

I just thought I might give a quick help with the instr part.

I haven't read the rest of the thread very carefully, so this is pretty much a minimal help.

The function InStr returns the position of the second string in the first.

o = InStr(cl.Formula, ",")

means that o is the position of the first "," in cl.Formula.

s = InStr(cl.Formula, ")")

is the position of the first ")"


Mid is the string which begins in the second argument, and have the length of the third argument.
This means that

z = Mid(cl.Formula, o + 1, s - 1 - o)

returns the part of cl.Formula that is between the first comma (o+1) and the first right parenthesis (s-1-o).

So in "(3,4)" this will return 4.

I have no idea what the code's for... I'm just telling you what it do.
__________________
/Niklas Jansson
Vas is offline   Reply With Quote
Old May 15th, 2002, 09:22 AM   #9
Lisa
Board Regular
 
Join Date: Apr 2002
Location: Indianapolis
Posts: 80
Default

Thanks, Vas. That does make sense with Nate's code... I couldn't for the life of me figure it out. The help files, especially for a novice, can be very confusing.

Lisa
Lisa is offline   Reply With Quote
Old May 15th, 2002, 09:38 AM   #10
Lisa
Board Regular
 
Join Date: Apr 2002
Location: Indianapolis
Posts: 80
Default

Nate -

I think I've got a pattern!

The first group that should have been deleted was, the next one wasn't. Then the one after that one (3) was, the next one (4) wasn't. Etc...

Lisa
Lisa 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 05:49 AM.


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