Consulting

Results 1 to 5 of 5

Thread: Solved: Selectively copy ActiveCell onChange

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Solved: Selectively copy ActiveCell onChange

    How can you selectively copy the active cell.value to another worksheet
    ("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1)
    only if it was BLANK before you changed its value.
    In other words, if the cell had a value in it before you changed it, I dont want to copy it.

    Thanks
    Last edited by Aussiebear; 04-13-2023 at 12:05 AM. Reason: Adjusted the code tags

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Option Explicit
    Dim OldVal
     
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    OldVal = Target.Value
    End Sub
     
    Private Sub Worksheet_Change(ByVal Target As Range)
    If OldVal = "" Then Target.Copy Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1)
    End Sub
    Last edited by Aussiebear; 04-13-2023 at 12:06 AM. Reason: Adjusted the code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Option Explicit
    
    Private mPrev As Variant
    Const WS_RANGE As String = "H1"     '<== change to suit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ws_exit
        Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
       With Target
          If mPrev = "" Then
             Target.Copy Worksheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1)
          End If
       End With
        End If
    ws_exit:
        Application.EnableEvents = True
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
       mPrev = Target.Value
        End If
    End Sub

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    Last edited by Aussiebear; 04-13-2023 at 12:07 AM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Selectively copy ActiveCell onChange

    Thanks all.

    I got xld ' s code to work perfectly.

    Wasn't quite able to follow all the logic of mdmackillop's code as the range area wasn't quite clear.

    Thanks again

  5. #5

    Worksheet_Change to apply for all Sheets

    Hi,

    Worksheet_Change can apply on the respective sheet tab to fire this event. But I want this event to fire for all sheets to effect because I am using .xlam file for this task.

    Pls give the solution

    Thanks,
    Divakar.

Posting Permissions

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