Two input cells equal to each other

2019-09-21 08:05发布

Is it possible to create two input cells that are equal to each other without VBA code eg. A1 = A5 and A5 = A1. So when I change the value of A1 it affects A5 and change in A5 makes A1 change too.

3条回答
We Are One
2楼-- · 2019-09-21 08:19

No, it is not. It will require VBA.

A cell can have a formula or it can have a user supplied value but not both.

The only way is VBA.

You can have a third cell, say B1. Then have the user change B1 and equate A1 and A5 to B1:

=B1

Then the user only needs to change one cell and the others will update.

查看更多
Luminary・发光体
3楼-- · 2019-09-21 08:20

Strictly speaking it is possible, if the sheet is set to use iterative calculation, with using =B1 in A5 and =A5 in B1. However entering data in either cell will overwrite the formula, so would only work the one time.

查看更多
冷血范
4楼-- · 2019-09-21 08:20

Place the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A1 As Range, A5 As Range
    Set A1 = Range("A1")
    Set A5 = Range("A5")
    If Not Intersect(A1, Target) Is Nothing Then
        Application.EnableEvents = False
            A1.Copy A5
        Application.EnableEvents = True
        Exit Sub
    End If

    If Not Intersect(A5, Target) Is Nothing Then
        Application.EnableEvents = False
            A5.Copy A1
        Application.EnableEvents = True
        Exit Sub
    End If
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

查看更多
登录 后发表回答