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 Apr 4th, 2002, 05:51 PM   #1
bobmc
 
Join Date: Mar 2002
Posts: 142
Default

I have a spreadsheet that utilizes VB code for specific cells. I've noticed that when I delete or insert a row above the cells affected by VB, the code does not change to reflect the relative change in cell numbers, so I've been fixing the code manually.

Any way to automate this?

Thanks.
bobmc is offline   Reply With Quote
Old Apr 4th, 2002, 05:55 PM   #2
willlobb
 
Join Date: Mar 2002
Location: Isle Of Man
Posts: 87
Default

what is your code?
Will
willlobb is offline   Reply With Quote
Old Apr 4th, 2002, 06:02 PM   #3
bobmc
 
Join Date: Mar 2002
Posts: 142
Default

Here's a snippet of it. There are 20 - 30 other instances of this in the code.
**************
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AE$13" Then [AE14,AE15].ClearContents
If Target.Address = "$AE$14" Then [AE13,AE15].ClearContents
If Target.Address = "$AE$15" Then [AE13,AE14].ClearContents
End Sub
*****************

Basically, the code just toggles a selection between three cells.

So, is it possible to make this dynamic so that if I delete say, row AA, these codes would automatically change from "AE" to "AD"?

Any help is greatly appreciated.

[ This Message was edited by: bobmc on 2002-04-04 15:48 ]
bobmc is offline   Reply With Quote
Old Apr 4th, 2002, 09:49 PM   #4
bobmc
 
Join Date: Mar 2002
Posts: 142
Default

Anyone?
bobmc is offline   Reply With Quote
Old Apr 4th, 2002, 11:16 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-04 15:49, bobmc wrote:
Anyone?
I don't think this is possible, because I keep getting caught in an endless loop when I clear the contents in the "other cells"

The entry in cell 1 clears the other two, but that clearing triggers the worksheet_change event again and again.

Here is what I have so far. Possibly otehrs can take this and run with it.

-------------------
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column > 26 And Target.Column < 32 Then
If Target.Row = 13 Then
Cells(14, Target.Column).ClearContents
Cells(15, Target.Column).ClearContents
Application.EnableEvents = False
Exit Sub
End If
If Target.Row = 14 Then
Cells(13, Target.Column).ClearContents
Cells(15, Target.Column).ClearContents
Application.EnableEvents = False
Exit Sub
End If
If Target.Row = 15 Then
Cells(13, Target.Column).ClearContents
Cells(14, Target.Column).ClearContents
Application.EnableEvents = False
Exit Sub
End If
End If
Application.EnableEvents = True
End Sub
--------------------------

This may get someone started.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 4th, 2002, 11:29 PM   #6
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

I spoke too soon. This worked for me:

In a normal code module, place the following three subs

----------------
Sub clear1(sValue)
Application.EnableEvents = False
Range(sValue).Offset(1, 0).ClearContents
Range(sValue).Offset(2, 0).ClearContents
Application.EnableEvents = True
End Sub

Sub clear2(sValue)
Application.EnableEvents = False
Range(sValue).Offset(-1, 0).ClearContents
Range(sValue).Offset(1, 0).ClearContents
Application.EnableEvents = True
End Sub

Sub clear3(sValue)
Application.EnableEvents = False
Range(sValue).Offset(-2, 0).ClearContents
Range(sValue).Offset(-1, 0).ClearContents
Application.EnableEvents = True
End Sub
-----------------

In your worksheet_change event place the following:

---------------
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column > 26 And Target.Column < 32 Then
If Target.Row = 13 Then
Call Module2.clear1(Target.Address)
End If
If Target.Row = 14 Then
Call Module2.clear2(Target.Address)
End If
If Target.Row = 15 Then
Call Module2.clear3(Target.Address)
End If
End If
End Sub
--------------------

Change the target.column range to suit your needs.

Post back if this doesn't work for you.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 4th, 2002, 11:30 PM   #7
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,639
Default

To respond to the last part, naming the column may be of serious use (with non-absolute references) (instert->names->define). Say you name the column "myClm", then you can try and use a procedure like:

Code:
Dim n As Integer, coL As Range
Set coL = Range("myClm")
n = coL.column
Worksheets(coL.Worksheet.Name).Range(n & ":" & n).Delete
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old Apr 4th, 2002, 11:34 PM   #8
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-04 17:30, NateO wrote:
To respond to the last part, naming the column may be of serious use (with non-absolute references) (instert->names->define). Say you name the column "myClm", then you can try and use a procedure like:

Code:
Dim n As Integer, coL As Range
Set coL = Range("myClm")
n = coL.column
Worksheets(coL.Worksheet.Name).Range(n & ":" & n).Delete
Hi NateO,

"To respond to the last part,..." were you referring to my first attempt or the second attempt (or both, or the OP)? No matter, because that was clever.

Awesome suggestion! I think it would work nicely.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 4th, 2002, 11:38 PM   #9
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,639
Default

Hi Jay, Thanks for the kudos! As always, don't mean to steal anyone's thunder, just thinking out loud. I was actually responding to:

Quote:
So, is it possible to make this dynamic so that if I delete say, row AA, these codes would automatically change from "AE" to "AD"?
My brain-storming began before I noticed your post. It seems to me one could shift this way.

Unfortunately, I'm at a thin-client without Excel, so it's hard for me to comment on your code, but having seen a post or two of yours (Jay), I suspect it works.

Have a good eve.

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-04-04 17:45 ]
NateO is offline   Reply With Quote
Old Apr 4th, 2002, 11:47 PM   #10
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-04 17:38, NateO wrote:
Hi Jay, don't mean to steal anyone's thunder, just thinking out loud. I was actually responding to:

Quote:
So, is it possible to make this dynamic so that if I delete say, row AA, these codes would automatically change from "AE" to "AD"?
It seems to me one could shift this way.

Unfortunately, I'm at a thin-client without Excel, so it's hard for me to comment on your code, but having seen a post or two of yours (Jay), I suspect it works.

Have a good eve.
No thunder to be stolen here. I think it would be a great solution, and if it doesn't work alone, it can be combined with my suggestions to make a robust option for the OP.

I'm outta here!
Jay
Jay Petrulis 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 +1. The time now is 08:46 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 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