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 Mar 19th, 2002, 08:12 AM   #1
kojak43
Board Regular
 
kojak43's Avatar
 
Join Date: Feb 2002
Posts: 270
Default

Over a month ago someone wrote in about this subject and the follwing code was furnished.

"Using something like this

Code:


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
Range("A1:A5").Sort Range("A1"), xlAscending, Header:=xlYes
End If
End Sub


In Here, I'm sorting Range("A1:A5") each time one of the cells changes. I'm also assuming it has a title.

End of supplied code"

My confusion is the range. I would have to increase the range for my sheet, but how can I make the range grow as my rows increase?

kojak43 is offline   Reply With Quote
Old Mar 19th, 2002, 08:20 AM   #2
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi


I would suggest a dynamic named range (well worth learning) and then using something like:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("MyDynaRange")) Is Nothing Then
Range("MyDynaRange").Sort Range("A1"), xlAscending, Header:=xlYes
End If
End Sub

See: http://www.ozgrid.com/Excel/DynamicRanges.htm

Or a second preference might be:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1", Range("A65336").End(xlUp))) Is Nothing Then
Range("A1", Range("A65336").End(xlUp)).Sort Range("A1"), xlAscending, Header:=xlYes
End If
End Sub


If the growing/shrinking range is column A





_________________
Kind Regards
Dave Hawley
OzGrid Business Applications
Microsoft Excel/VBA Training


[ This Message was edited by: Dave Hawley on 2002-03-19 07:21 ]

[ This Message was edited by: Dave Hawley on 2002-03-19 07:25 ]
Dave Hawley is offline   Reply With Quote
Old Mar 19th, 2002, 08:53 AM   #3
kojak43
Board Regular
 
kojak43's Avatar
 
Join Date: Feb 2002
Posts: 270
Default

Thanks for the prompt reply.
I am using '97
I entered your 2nd code and tested. No joy.
Message pop-up says Ambigious Name detected Worksheeet_Change. Help tells me Excel can not find, within VBA, veenlr3.hlp. A search of my hard drive says I have, within VBA, veendf3.hlp.
Guidance?
Oh by the way, went to your web site. I will spend hours there. It seems to be nicely done.
kojak43 is offline   Reply With Quote
Old Mar 19th, 2002, 08:57 AM   #4
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

It just means you already have a Procedure with that name. Delete the old one and paste in mine by, right clicking on the sheet name tab and selecting "View Code"
Dave Hawley is offline   Reply With Quote
Old Mar 19th, 2002, 09:11 AM   #5
kojak43
Board Regular
 
kojak43's Avatar
 
Join Date: Feb 2002
Posts: 270
Default

Again thanks. Rem'd out the other code. (Thanks for the right click view code tip)

Column "A" range starts at A6.
Column "B" is the field I want sorted.
I changed your code. Mine says "A6" for o/a range and SortRange is "B6"

Test in a new number at "B" and it does not resort after all data has been stroked in and I press enter.


kojak43 is offline   Reply With Quote
Old Mar 19th, 2002, 06:23 PM   #6
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

No too sure I follow waht you have said, but try this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B6", Range("B65336").End(xlUp))) Is Nothing Then
Range("B6", Range("B65336").End(xlUp)).Sort Range("B6"), xlAscending, Header:=xlYes
End If
End Sub


As I said intitially the Dynamic range option would be best.


Dave Hawley is offline   Reply With Quote
Old Mar 19th, 2002, 08:57 PM   #7
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

I think I recongize this code

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
Range("A1:A5").Sort Range("A1"), xlAscending, Header:=xlYes
End If
End Sub

I don't feel (Altough agree with Dave, it's worth learning them !) that a Dynamic range is needed here... just a little object programming:

Try this code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng as Range
Set Rng = Range("A1","B" & Range("A65536").End(xlUp).Row)

If Not Intersect(Target, Rng) Is Nothing Then
Rng.Sort Range("A1"), xlAscending, Header:=xlYes
End If
End Sub

__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old Mar 20th, 2002, 07:15 AM   #8
kojak43
Board Regular
 
kojak43's Avatar
 
Join Date: Feb 2002
Posts: 270
Default

Thank you both.
It still is not quite right. And I apologize for not providing the following detail previously.
A1:L5 contain various things as instructions, title, date, down arrows for filters etc. The column "headers" appear on A5:L5
Actual data begins on A6:L6
The field that needs to be sorted starts at B6.
Both of your codes sort column B. They (the codes) do not drag the other data along with it. When job number in Column "B" moves to the correct position, it replaces another number in the same column, and the other data stays in the same place. So the job number for one client now has the data for a different client.

My wish list would be to keystroke in all data Ax:Lx (below A5:L5) press enter,tab or mouse click to the next row,and have that entire line of data sort to the correct position based on the number in column "B"

Thanks for your guidance
kojak43 is offline   Reply With Quote
Old Mar 20th, 2002, 12:15 PM   #9
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Try this one.
This will not sort anything before column L is changed / written to. Then it will sort on column B / A /C from row 6 and down.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 12 And Target.Row > 5 Then
Selection.Sort Key1:=Range("B6"), Order1:=xlAscending, Key2:=Range("A6") _
, Order2:=xlAscending, Key3:=Range("C6"), Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End If
End Sub

regards Tommy

Tommy Bak is offline   Reply With Quote
Old Mar 20th, 2002, 01:58 PM   #10
kojak43
Board Regular
 
kojak43's Avatar
 
Join Date: Feb 2002
Posts: 270
Default

Tommy, this code does not seem to work at all.
I can see that it waits for something in column 12 in any row greater than 5, but when I get to 12, and press enter, the entire line stays put.

If only data in columns "A", "B" and "C" are mentioned, how does the other data (the stuff in "E"-"N") know to move when it sorts?
kojak43 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 12:45 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