Two input cells equal to each other

為{幸葍}努か 提交于 2020-01-07 09:07:58

问题


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.


回答1:


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.




回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/53174834/two-input-cells-equal-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!