Share via

VBA Code Help for Circular Reference

Anonymous
2019-12-11T16:30:57+00:00

Good morning. Need some VBA help, if you don't mind. I'm needing a VBA code for a circular reference(?) and which works when there is data in either cell (remains blank if not). For example: if A1 has a word or content, then D1's content is that of A1, and if D1 has a word entered, then A1's content is that of D1 (otherwise the cells stay blank). I need to be able to type in either cell at any time without messing up or overriding the circular reference. Hope that makes sense. Thank you in advance.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2019-12-11T21:21:20+00:00

Remove the line

    If Target.Count > 1 Then Exit Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-12-11T21:08:13+00:00

    My D1 is a group of merged cells (D1 is the start), which appears to be the problem. If I make a new book with only A1 and D1, it works perfectly. (I tried naming my range and substituting that name for "D1" but it still didn't work.) What would be the fix for that?

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2019-12-11T20:44:27+00:00

    When I try the code, clearing D1 also clears A1. Could you please check again? If the problem remains, please make a sample workbook available through Dropbox, Google Drive or OneDrive.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-12-11T20:23:49+00:00

    Thanks, HansV. When typing in either: if I clear A1, then D1 clears, but if I clear D1, A1 still remains. Is there a fix for that?

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2019-12-11T19:37:56+00:00

    Don't use formulas, only code.

    Right-click the sheet tab and select 'View Code' from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_Change(ByVal Target As Range)

        If Target.Count > 1 Then Exit Sub

        If Not Intersect(Range("A1"), Target) Is Nothing Then

            Application.EnableEvents = False

            Range("D1").Value = Range("A1").Value

            Application.EnableEvents = True

        ElseIf Not Intersect(Range("D1"), Target) Is Nothing Then

            Application.EnableEvents = False

            Range("A1").Value = Range("D1").Value

            Application.EnableEvents = True

        End If

    End Sub

    Was this answer helpful?

    0 comments No comments