Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Two Way Cell Value Connection A1-C1 and C1-A1

Hello,

I am wondering if anyone knows a visual basic method to update the
values of two cells in visual basic so one is equal to the other if it
changes.

For example, if I change A1 to be "5" I want C1 to be updated to "5"
_and_ if I change C1 to be "5" I want A1 to be updated to "5" as well.

I've been searching for a cell(s).change option but was not able to
find that function, does anyone have any suggestions or guesses?

Thanks everyone for your help!

Shaun

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Two Way Cell Value Connection A1-C1 and C1-A1

Hi Shaun,

Try this in the Worksheet module (right click the sheet tab View code)

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo errH
If Not Intersect(Target, Range("A1,C1")) Is Nothing Then
Application.EnableEvents = False
If Target(1).Address = "$A$1" Then
Range("C1").Value = Range("A1").Value
Else
Range("A1").Value = Range("C1").Value
End If
End If

errH:
Application.EnableEvents = True
End

Regards,
Peter T

"Shaun" wrote in message
oups.com...
Hello,

I am wondering if anyone knows a visual basic method to update the
values of two cells in visual basic so one is equal to the other if it
changes.

For example, if I change A1 to be "5" I want C1 to be updated to "5"
_and_ if I change C1 to be "5" I want A1 to be updated to "5" as well.

I've been searching for a cell(s).change option but was not able to
find that function, does anyone have any suggestions or guesses?

Thanks everyone for your help!

Shaun



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Two Way Cell Value Connection A1-C1 and C1-A1

Right click on the sheet tab and paste in code like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count 1 Then Exit Sub
On Error GoTo ErrHandler
If Target.Address = "$A$1" Then
Application.EnableEvents = False
Range("C1") = Target.Value
ElseIf Target.Address = "$C$1" Then
Application.EnableEvents = False
Range("A1").Value = Target.Value
End If
ErrHandler:
Application.EnableEvents = True

End Sub

for general information on Excel Events see Chip Pearson's site
http://www.cpearson.com/excel/events.htm

--
Regards,
Tom Ogilvy

"Shaun" wrote in message
oups.com...
Hello,

I am wondering if anyone knows a visual basic method to update the
values of two cells in visual basic so one is equal to the other if it
changes.

For example, if I change A1 to be "5" I want C1 to be updated to "5"
_and_ if I change C1 to be "5" I want A1 to be updated to "5" as well.

I've been searching for a cell(s).change option but was not able to
find that function, does anyone have any suggestions or guesses?

Thanks everyone for your help!

Shaun



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default Two Way Cell Value Connection A1-C1 and C1-A1

This code needs to be pasted into the sheet you want to use it on, not into a
general module...

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Errorhandler
Application.EnableEvents = False
If Target.Address = "$A$1" Then
Range("C1").Value = Target.Value
ElseIf Target.Address = "$C$1" Then
Range("A1").Value = Target.Value
End If
Errorhandler:
Application.EnableEvents = True
End Sub

--
HTH...

Jim Thomlinson


"Shaun" wrote:

Hello,

I am wondering if anyone knows a visual basic method to update the
values of two cells in visual basic so one is equal to the other if it
changes.

For example, if I change A1 to be "5" I want C1 to be updated to "5"
_and_ if I change C1 to be "5" I want A1 to be updated to "5" as well.

I've been searching for a cell(s).change option but was not able to
find that function, does anyone have any suggestions or guesses?

Thanks everyone for your help!

Shaun


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Two Way Cell Value Connection A1-C1 and C1-A1

Hi,

Thanks to Peter T, Tom Ogilvy & Jim Thomlinson for your replies!

All three methods work :D! (Peter T's needs End Sub instead of End and
then it works perfect).

It is very neat to see it autoupdate, this is very useful. I can have a
master input sheet that has updatable figures now.

Bye,

Shaun



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default Two Way Cell Value Connection A1-C1 and C1-A1

Tom's code is probably the best of the bunch (I should have done a count of
the target cells as Tom's code does.)... I would go with that. It may be
important to note that values pasted into A1 or C1 will not trigger the
macro. Just a warning.
--
HTH...

Jim Thomlinson


"Shaun" wrote:

Hi,

Thanks to Peter T, Tom Ogilvy & Jim Thomlinson for your replies!

All three methods work :D! (Peter T's needs End Sub instead of End and
then it works perfect).

It is very neat to see it autoupdate, this is very useful. I can have a
master input sheet that has updatable figures now.

Bye,

Shaun


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Two Way Cell Value Connection A1-C1 and C1-A1

Just some added info:

In xl2003 at least, copying a single cell to either A1 or C1 caused the
change event to run.

with my code, copying a multicell range to a destination that included A1 or
C1 would cause it not to run because of the If Target.count 1 then exit
sub in my code.

It really depends on what you want to account for whether that line is
useful or not.

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote in
message ...
Tom's code is probably the best of the bunch (I should have done a count

of
the target cells as Tom's code does.)... I would go with that. It may be
important to note that values pasted into A1 or C1 will not trigger the
macro. Just a warning.
--
HTH...

Jim Thomlinson


"Shaun" wrote:

Hi,

Thanks to Peter T, Tom Ogilvy & Jim Thomlinson for your replies!

All three methods work :D! (Peter T's needs End Sub instead of End and
then it works perfect).

It is very neat to see it autoupdate, this is very useful. I can have a
master input sheet that has updatable figures now.

Bye,

Shaun




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Two Way Cell Value Connection A1-C1 and C1-A1

In xl2003 at least, copying a single cell to either A1 or C1 caused the
change event to run.


Similarly in my xl2000, also Cut

None of the three examples catered for all situations of how a cell might be
changed, particularly if changing multiple cells, eg pasting or deleting
(incl Cut) and the first selected cell (ie activecell) is not A1 or C1. Mine
catered perhaps for more scenarios using Target(1) but only because the
cells in question were in row 1.

Following attempts to equalise B & D in rows 2-20 even if the active cell is
not in these ranges at time of change and/or multiple cells changed in one
go, overkill !

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, c As Long

' On Error Resume Next
Set Rng = Intersect(Target, Range("B2:B20"))

If Not Rng Is Nothing Then
c = 2
Else
Set Rng = Intersect(Target, Range("D2:D20"))
If Not Rng Is Nothing Then
c = -2
End If
End If

If c Then
On Error GoTo ErrH
Application.EnableEvents = False
Rng.Offset(0, c).Value = Rng.Value
End If

ErrH:
Application.EnableEvents = True
End Sub

Shaun - for your particular purposes change the ranges to "A1" & "C1"
respectively

Regards,
Peter T

PS hope I've included "Sub" after "End" this time !


"Tom Ogilvy" wrote in message
...
Just some added info:

In xl2003 at least, copying a single cell to either A1 or C1 caused the
change event to run.

with my code, copying a multicell range to a destination that included A1

or
C1 would cause it not to run because of the If Target.count 1 then exit
sub in my code.

It really depends on what you want to account for whether that line is
useful or not.

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote in
message ...
Tom's code is probably the best of the bunch (I should have done a count

of
the target cells as Tom's code does.)... I would go with that. It may be
important to note that values pasted into A1 or C1 will not trigger the
macro. Just a warning.
--
HTH...

Jim Thomlinson


"Shaun" wrote:

Hi,

Thanks to Peter T, Tom Ogilvy & Jim Thomlinson for your replies!

All three methods work :D! (Peter T's needs End Sub instead of End and
then it works perfect).

It is very neat to see it autoupdate, this is very useful. I can have

a
master input sheet that has updatable figures now.

Bye,

Shaun






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
Maintain lowest value in an "external connection" cell Nick Ng[_2_] Excel Worksheet Functions 0 March 30th 10 11:33 PM
always recheck data connection library for latest connection strin FurmanGG Excel Discussion (Misc queries) 0 September 14th 07 04:48 PM
no DDE connection?? askmiller Excel Discussion (Misc queries) 0 August 5th 06 09:19 PM
Connection using ADO alvin Excel Programming 5 April 5th 05 07:58 AM
Making a connection: shape clicked and cell it lies over. Gazza Excel Programming 2 February 12th 04 02:26 PM


All times are GMT +1. The time now is 03:19 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"