Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
davidhub
 
Posts: n/a
Default how can i change a value in a cell from a formula in another cell?


  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

A formula can only update the value in the cell that holds it.



davidhub wrote:

--

Dave Peterson
  #3   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Tue, 5 Jul 2005 05:00:01 -0700, "davidhub"
wrote:

In the cell that you wish to change (e.g. A2) enter a formula that refers to
the cell with the formula (e.g. A1)

For example, in cell A2 enter:

=A1

If having a formula in A2 is not acceptable, then you will need to use a VBA
solution. Using an event macro, you can check the contents of A1 and write an
appropriate number into A2.

To enter the VB routine, right click on the worksheet tab and select View Code.

Then paste the code below into the window that opens.

Make the appropriate changes in your cell reference assignments.

==============================
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellToBeUpdated As Range
Dim CellToCheck As Range

Set CellToBeUpdated = Range("A2")
Set CellToCheck = Range("A1")

CellToBeUpdated.Value = CellToCheck.Value

End Sub
===========================


--ron
  #4   Report Post  
Tom Ogilvy
 
Posts: n/a
Default

Just a heads up on a potential problem with the code suggested.

If you modify the macro slightly to illustrate the problem and enter a
single value in any cell, you will see, that it runs recursively:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellToBeUpdated As Range
Dim CellToCheck As Range
Static cnt As Long
cnt = cnt + 1
Debug.Print cnt
Set CellToBeUpdated = Range("A2")
Set CellToCheck = Range("A1")

CellToBeUpdated.Value = CellToCheck.Value

End Sub

Last number for cnt on a single cell entry was 228 (so Excel senses the
problem and corrects after 228 runs).

to prevent this, you need to suppress events


Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellToBeUpdated As Range
Dim CellToCheck As Range
On Error goto ErrHandler:
Set CellToBeUpdated = Range("A2")
Set CellToCheck = Range("A1")
Application.EnableEvents = False
CellToBeUpdated.Value = CellToCheck.Value
ErrHandler:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy

"Ron Rosenfeld" wrote in message
...
On Tue, 5 Jul 2005 05:00:01 -0700, "davidhub"
wrote:

In the cell that you wish to change (e.g. A2) enter a formula that refers

to
the cell with the formula (e.g. A1)

For example, in cell A2 enter:

=A1

If having a formula in A2 is not acceptable, then you will need to use a

VBA
solution. Using an event macro, you can check the contents of A1 and

write an
appropriate number into A2.

To enter the VB routine, right click on the worksheet tab and select View

Code.

Then paste the code below into the window that opens.

Make the appropriate changes in your cell reference assignments.

==============================
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellToBeUpdated As Range
Dim CellToCheck As Range

Set CellToBeUpdated = Range("A2")
Set CellToCheck = Range("A1")

CellToBeUpdated.Value = CellToCheck.Value

End Sub
===========================


--ron



  #5   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default

On Tue, 5 Jul 2005 09:45:06 -0400, "Tom Ogilvy" wrote:

Just a heads up on a potential problem with the code suggested.


Thanks for pointing that out. I usually do remember to DisableEvents.
Brainf&&t this time around.


--ron


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional cell shading when a change occurs zooeyhall Excel Discussion (Misc queries) 1 June 6th 05 05:14 PM
SQL - Auto Refresh Upon Cell Value Change Elliot J Excel Worksheet Functions 1 May 12th 05 10:22 AM
How do I create formula to change cell color excel formula Excel Worksheet Functions 2 December 29th 04 08:13 PM
How can I write an if-then formula for 0 or less than 0 in cell t. Baz1 Excel Worksheet Functions 1 November 30th 04 04:33 PM
Conditional Formatting for dates spacerocket Excel Worksheet Functions 2 November 4th 04 10:13 AM


All times are GMT +1. The time now is 04:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"