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, 07:22 AM   #1
blentz2
New Member
 
Join Date: Apr 2002
Posts: 3
Default

I have a large macro that I only what to run when a specific cell has changed. This cell G4 is a date. When the used changes it, I want to run the macro which will change about 100 cell formulas to reflect the new date. Again only want to run macro when this particlar cell changes.

[ This Message was edited by: blentz2 on 2002-04-04 06:22 ]
blentz2 is offline   Reply With Quote
Old Apr 4th, 2002, 07:29 AM   #2
lenze
MrExcel MVP
 
lenze's Avatar
 
Join Date: Feb 2002
Location: Helena, MT
Posts: 13,690
Default

You can do this with the WorkSheet Change Event.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$4" Then YourMacro
End Sub
lenze is offline   Reply With Quote
Old Apr 4th, 2002, 07:30 AM   #3
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Hi blentz2

Right click your sheet tab, left click View Code and paste this code in the white area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then Application.Run "my macro name"
End Sub

change "my macro name" to the actual name of the macro you want to run

Hope this helps
regards
Derek
Derek is offline   Reply With Quote
Old Apr 4th, 2002, 07:37 AM   #4
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

This test code will add a blank row below the trigger cell A1 if any change is made to A1. You can modify this code to do what you want. This sub is added to the sheet module. JSW

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Trigger cell address.
n = Range("A1").Value

'Cell to act upon.
If Target.Address = "$A$1" Then
n = n + 1
Range("A2").Value = n

'What to do to the new cell.
'Move cursor.
Selection.Offset(rowOffset:=1, ColumnOffset:=0).Select
'Add blank row.
Selection.EntireRow.Insert
End If

End Sub
Joe Was is offline   Reply With Quote
Old Apr 4th, 2002, 01:25 PM   #5
blentz2
New Member
 
Join Date: Apr 2002
Posts: 3
Default

Derek,
Thank you for responding. I have tried to follow your example, but...I'm getting a Compile Error: Expected Function or variable MsgBox when I change cell G4. Here is my code (Private line in yellow):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then Application.Run DoM
End Sub
Partial DoM Macro:
Private Sub DoM()
'
' DoM Macro
' Macro recorded 3/21/2001 by Bob Lentz
'

'
Dim DayOfMonth As String
Range("G4").Select
DayOf = Format(Day(ActiveCell.Value), "dd") + 1
' MsgBox (DayOf)
If (DayOf > 31) Then DayOf = 33 - DayOf
' MsgBox (DayOf)
If (DayOf < 10) Then DayOfMonth = "0" & DayOf Else DayOfMonth = DayOf
' MsgBox (DayOfMonth)
Range("E34").Select
ActiveCell.FormulaR1C1 = "=R[-1]C/SUM('01:" & DayOfMonth & "'!R[-30]C)"
Range("E35").Select
ActiveCell.FormulaR1C1 = "=SUM('01:" & DayOfMonth & "'!R[-30]C)"
...
As you may assume I am new to macros...

Quote:
On 2002-04-04 06:30, Derek wrote:
Hi blentz2

Right click your sheet tab, left click View Code and paste this code in the white area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then Application.Run "my macro name"
End Sub

change "my macro name" to the actual name of the macro you want to run

Hope this helps
regards
Derek
blentz2 is offline   Reply With Quote
Old Apr 5th, 2002, 06:24 AM   #6
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Hi Bob
The event macro I gave you refers to the name of your normal module macro (it is not part of the event macro). You need to refer to the name of that macro as it appears at the top of its code and enclose it in double quotes like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then Application.Run "DoM()"
End Sub

This event macro is then just a trigger that sets off your module macro "DoM()" when you make a change in G4. This is a more flexible way to do it since you can then also run "DoM()" independantly via a button or whatever.

If you want the whole lot as an event macro word it like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then
Dim DayOfMonth As String
Range("G4").Select
DayOf = Format(Day(ActiveCell.Value), "dd") + 1
' MsgBox (DayOf)
If (DayOf > 31) Then DayOf = 33 - DayOf
' MsgBox (DayOf)
If (DayOf < 10) Then DayOfMonth = "0" & DayOf Else DayOfMonth = DayOf
' MsgBox (DayOfMonth)
Range("E34").Select
ActiveCell.FormulaR1C1 = "=R[-1]C/SUM('01:" & DayOfMonth & "'!R[-30]C)"
Range("E35").Select
ActiveCell.FormulaR1C1 = "=SUM('01:" & DayOfMonth & "'!R[-30]C)"
...
End If
End Sub


Note that because the first line ie - "If Target.Address = "G$4" Then" - goes to a new line after "Then" you will need to add the line "End If" after your "DoM()" code, as a statement that it has reached the end of the action which was dependant upon the if condition.

Hope this makes sense
regards
Derek








Derek is offline   Reply With Quote
Old Apr 5th, 2002, 07:21 AM   #7
blentz2
New Member
 
Join Date: Apr 2002
Posts: 3
Default

Quote:
On 2002-04-05 05:24, Derek wrote:
Hi Bob
The event macro I gave you refers to the name of your normal module macro (it is not part of the event macro). You need to refer to the name of that macro as it appears at the top of its code and enclose it in double quotes like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then Application.Run "DoM()"
End Sub

This event macro is then just a trigger that sets off your module macro "DoM()" when you make a change in G4. This is a more flexible way to do it since you can then also run "DoM()" independantly via a button or whatever.

If you want the whole lot as an event macro word it like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "G$4" Then
Dim DayOfMonth As String
Range("G4").Select
DayOf = Format(Day(ActiveCell.Value), "dd") + 1
' MsgBox (DayOf)
If (DayOf > 31) Then DayOf = 33 - DayOf
' MsgBox (DayOf)
If (DayOf < 10) Then DayOfMonth = "0" & DayOf Else DayOfMonth = DayOf
' MsgBox (DayOfMonth)
Range("E34").Select
ActiveCell.FormulaR1C1 = "=R[-1]C/SUM('01:" & DayOfMonth & "'!R[-30]C)"
Range("E35").Select
ActiveCell.FormulaR1C1 = "=SUM('01:" & DayOfMonth & "'!R[-30]C)"
...
End If
End Sub


Note that because the first line ie - "If Target.Address = "G$4" Then" - goes to a new line after "Then" you will need to add the line "End If" after your "DoM()" code, as a statement that it has reached the end of the action which was dependant upon the if condition.

Hope this makes sense
regards
Derek
Derek,
I have tried it both ways you suggested. I can not get the .change event to execute. G4 is a date field, if that matters. I want to change the date, let's say from 2/11/02 to 2/16/02. Then take the 16 and place it into formulas. The only way I can get it to work is to change date (G4) and run DoM manually. I have uncommented MsgBox statements within event version and get nothing...What silly thing am I missing????
TIA







blentz2 is offline   Reply With Quote
Old Apr 6th, 2002, 11:27 PM   #8
Derek
Board Regular
 
Join Date: Feb 2002
Location: Perth Australia
Posts: 1,567
Default

Bob

I think the answer is in the event macro.
The target address should read "$G$4" not "G$4" . Sorry, I didn't notice the typo.

regards
Derek
Derek 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 06:54 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