Results 1 to 20 of 51

Thread: Cell is equal to another cell until you write something in it

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Usually means that Application.EnableEvents was not disabled and the _Change event kept calling itself
    Yep, Probably what's happening.

    Sheet1 code:
    Option Explicit
    
    Private Sub Worksheet_Calculate()
    'Necessary with Formulas in A1
            CheckB1 
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.enableEvents = False
            If Not Intersect(Target, Range("A1")) Is Nothing Then CheckB1 'A1 changed
    Application.enableEvents = True
    End Sub
    
    Private Sub CheckB1()
            If Sheet2.Range("B1") = "" Then Sheet2.Range("B1") = Range("A1")
    End Sub
    Sheet2 code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.enableEvents = False
            If Not Intersect(Target, Range("B1")) Is Nothing Then CheckA1 'B1 changed
    Application.enableEvents = True
    End Sub
    
    Private Sub CheckA1()
        If Range("B1") = "" Then Range(" B1") = Sheet2.Range("A1")
    End Sub

    BTW, with With Target.Cells(1, 1), What happens when Range("A1:C1") = Range("A2:C2")

    That's why I like If Not Intersect(Target, Range("blah")) Is Nothing
    Please take the time to read the Forum FAQ

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    Quote Originally Posted by SamT View Post
    Yep, Probably what's happening.

    BTW, with With Target.Cells(1, 1), What happens when Range("A1:C1") = Range("A2:C2")

    That's why I like If Not Intersect(Target, Range("blah")) Is Nothing
    Probably habit

    My thinking is that (as you say) multiple cells might be selected, but I wanted to ensure that I'm acting on the one that I think I want to, so I test for the first cell in Target

    If Target was A1:C1 (3 cells changed), then Target.Cells(1,1) = A1

    If Target was A2:C2 (3 cells changed), then Target.Cells(1,1) = A2 so Exit Sub

    If Target was D10:F20 (33 cells changed) AND the desired action was to update (for ex) column G10:G20 with 2 x D1020, I'd use your way

    Again, just habit and personal preference.

    I'm not worried -- I'm sure someone here will tell me why it's wrong so maybe I'll learn some thing
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •