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 8th, 2002, 11:42 AM   #1
JohnJay
New Member
 
Join Date: Mar 2002
Posts: 37
Default

I am writing macros & VBA functions to do various tasks. But I wish to have cell references in the macros & VBA functions to be updates when a column or row is added to a sheet. Can anyone shed light on this?

Thank you
JohnJay is offline   Reply With Quote
Old Mar 8th, 2002, 11:53 AM   #2
Barrie Davidson
MrExcel MVP
 
Barrie Davidson's Avatar
 
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
Default

Quote:
On 2002-03-08 10:42, JohnJay wrote:
I am writing macros & VBA functions to do various tasks. But I wish to have cell references in the macros & VBA functions to be updates when a column or row is added to a sheet. Can anyone shed light on this?

Thank you
To identify columns, do you have a header row? If yes, you could search for a specific value and, when found, set your column at that point.

For your rows, I assume you want to be able to find the last row. If that is the case, you could use something like:

Dim LastRow As Long
LastRow = Range("A65536").End(xlUp).Row
Range("D" & LastRow).Select

This would select the cell in column D at the last row of your data (assuming that column A always contains data).

Regards,
__________________
Barrie Davidson

"You're only given a little spark of madness. You mustn't lose it." - Robin Williams
Barrie Davidson is offline   Reply With Quote
Old Mar 8th, 2002, 12:14 PM   #3
JohnJay
New Member
 
Join Date: Mar 2002
Posts: 37
Default

Barrie, actually what I am doing is trying to set up fuctions that do specific tasks to a sheet. So say I write a macro to perform sort on column K, but the user later adds a column before anywhere before K. Now the macro will sort what was column J before the insert. I hope this gives you a better understanding. By the way, I am using Headers, but the names are always subject to change....Unless I dont alow that.
JohnJay is offline   Reply With Quote
Old Mar 8th, 2002, 12:23 PM   #4
Barrie Davidson
MrExcel MVP
 
Barrie Davidson's Avatar
 
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
Default

Quote:
On 2002-03-08 11:14, JohnJay wrote:
Barrie, actually what I am doing is trying to set up fuctions that do specific tasks to a sheet. So say I write a macro to perform sort on column K, but the user later adds a column before anywhere before K. Now the macro will sort what was column J before the insert. I hope this gives you a better understanding. By the way, I am using Headers, but the names are always subject to change....Unless I dont alow that.
Are you able to lock the header values? If so, you could use something like this (that sorts on the header labelled "JohnJay"):
Code:
Dim SortColumn As Integer, LastColumn As Integer
Dim LastRow As Long
LastColumn = Range("A1").End(xlToRight).Column
LastRow = Range("A65536").End(xlUp).Row
SortColumn = ActiveSheet.Find(what:="JohnJay", _
    LookAt:=xlWhole).Column
Range(Cells(1, 1), Cells(LastRow, LastColumn)).Sort _
    Key1:=Range("A" & SortColumn), _
    Order1:=xlAscending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
Does this help you?
__________________
Barrie Davidson

"You're only given a little spark of madness. You mustn't lose it." - Robin Williams
Barrie Davidson is offline   Reply With Quote
Old Mar 8th, 2002, 12:35 PM   #5
JohnJay
New Member
 
Join Date: Mar 2002
Posts: 37
Default

Barrie, I am getting your idea, and I think it may work. Couple of questions though...
How can I lock the header, and the following part of your code is giving me an error in VBA

SortColumn = ActiveSheet.Find(what:="Division", _
LookAt:=xlWhole).Column
JohnJay is offline   Reply With Quote
Old Mar 8th, 2002, 12:42 PM   #6
Barrie Davidson
MrExcel MVP
 
Barrie Davidson's Avatar
 
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
Default

Quote:
On 2002-03-08 11:35, JohnJay wrote:
Barrie, I am getting your idea, and I think it may work. Couple of questions though...
How can I lock the header, and the following part of your code is giving me an error in VBA

SortColumn = ActiveSheet.Find(what:="Division", _
LookAt:=xlWhole).Column
Now that I think about it, you won't be able to lock the header and allow the user to insert a column. I was thinking you could lock the header row and then protect the worksheet, but if you protect the worksheet the user won't be able to insert a column. How about slightly changing the code to:

Sub SortData()
Dim SortColumn As Integer, LastColumn As Integer
Dim LastRow As Long
LastColumn = Range("A1").End(xlToRight).Column
LastRow = Range("A65536").End(xlUp).Row
On Error GoTo ErrorHandler
SortColumn = ActiveSheet.Find(what:="Division", _
LookAt:=xlWhole).Column
On Error GoTo 0
Range(Cells(1, 1), Cells(LastRow, LastColumn)).Sort _
Key1:=Range("A" & SortColumn), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Exit Sub
ErrorHandler:
MsgBox prompt:="Could not find a header labelled DIVISION" _
& Chr(13) & "Data was not sorted", _
Buttons:=vbCritical + vbOKOnly
End Sub

This will sort on "Division" and, if it's not found, will return an error message.

As for the error message you received, does "Division" exist in the spreadsheet. The macro is searching for an exact match to that word.

Regards,

__________________
Barrie Davidson

"You're only given a little spark of madness. You mustn't lose it." - Robin Williams
Barrie Davidson is offline   Reply With Quote
Old Mar 8th, 2002, 12:50 PM   #7
JohnJay
New Member
 
Join Date: Mar 2002
Posts: 37
Default

Barrie,

The error I get back when

"SortColumn = ActiveSheet.Find _(what:="Division", LookAt:=xlWhole).Column"

is executed is, "Object doesn't support this property or method (Error 438)" The error check that you put in seems like a great idea, the only thing is that it caught this error as "Division" not found.
JohnJay is offline   Reply With Quote
Old Mar 8th, 2002, 12:58 PM   #8
Barrie Davidson
MrExcel MVP
 
Barrie Davidson's Avatar
 
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
Default

Quote:
On 2002-03-08 11:50, JohnJay wrote:
Barrie,

The error I get back when

"SortColumn = ActiveSheet.Find _(what:="Division", LookAt:=xlWhole).Column"

is executed is, "Object doesn't support this property or method (Error 438)" The error check that you put in seems like a great idea, the only thing is that it caught this error as "Division" not found.
John, good thing you're patient.
Let's try this code instead:

Sub SortData()
Dim SortColumn As Integer, LastColumn As Integer
Dim LastRow As Long
LastColumn = Range("A1").End(xlToRight).Column
LastRow = Range("A65536").End(xlUp).Row
On Error GoTo ErrorHandler
SortColumn = Rows("1:1").Find(What:="Division", _
LookAt:=xlWhole).Column
On Error GoTo 0
Range(Cells(1, 1), Cells(LastRow, LastColumn)).Sort _
Key1:=Range("A" & SortColumn), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Exit Sub
ErrorHandler:
MsgBox prompt:="Could not find a header labelled DIVISION" _
& Chr(13) & "Data was not sorted", _
Buttons:=vbCritical + vbOKOnly
End Sub

I changed where the macro searches for "Division". "ActiveSheet" was an invalid object for the "Find" function and I further changed it to only search the first row (your headers) just in case the word "Division" might exist somewhere else in your data.

How close are we now?

__________________
Barrie Davidson

"You're only given a little spark of madness. You mustn't lose it." - Robin Williams
Barrie Davidson is offline   Reply With Quote
Old Mar 8th, 2002, 01:09 PM   #9
JohnJay
New Member
 
Join Date: Mar 2002
Posts: 37
Default

ok barrie, the find for the column worked great..One other problem. The sort key is ""A" & Sortcolumn" Which if my "Division" is in Column 11, the Key gives All. What I would like to happen is that it sorts the whole sheet according to the "Division" column, minus the header of course.

thanks a bunch for your help
JohnJay is offline   Reply With Quote
Old Mar 8th, 2002, 01:16 PM   #10
Barrie Davidson
MrExcel MVP
 
Barrie Davidson's Avatar
 
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
Default

John, sorry for the wild goose chase - I guess I must be getting old

Change that statement to read (I changed the Key1 part):
Range(Cells(1, 1), Cells(LastRow, LastColumn)).Sort _
Key1:=Range(Cells(1, SortColumn)), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom


Now, it's time I wake up and pay attention.
__________________
Barrie Davidson

"You're only given a little spark of madness. You mustn't lose it." - Robin Williams
Barrie Davidson 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 08:10 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