+ Reply to Thread
Results 1 to 8 of 8

VBA to add cell contents to another cell

Hybrid View

samcdavies VBA to add cell contents to... 08-02-2013, 06:04 AM
ragulduy Re: VBA to add cell contents... 08-02-2013, 06:08 AM
samcdavies Re: VBA to add cell contents... 08-02-2013, 06:12 AM
ragulduy Re: VBA to add cell contents... 08-02-2013, 06:16 AM
samcdavies Re: VBA to add cell contents... 08-02-2013, 06:20 AM
ragulduy Re: VBA to add cell contents... 08-02-2013, 06:23 AM
samcdavies Re: VBA to add cell contents... 08-02-2013, 06:28 AM
ragulduy Re: VBA to add cell contents... 08-02-2013, 06:29 AM
  1. #1
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    VBA to add cell contents to another cell

    Hi all

    I have data in column T (always numerical) between row 1 and 10,000 - everytime the number is changed, I'd like the number to be added to column CK.

    So for example T1 is changed to 40, cell CK1 is now 40.
    Then T1 is changed to 50, so CK1 is now 90.
    Then T1 changed to zero so CK1 remains at 90
    Then T1 is changed to 100 so CK1 becomes 190
    ...and so on forever....

    Is this possible?

    Thanks very much in advance!

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA to add cell contents to another cell

    in the worksheet object:
    Private Sub worksheet_change(ByVal target As Range)
    If Not Intersect(target, Range("T:T")) Is Nothing Then
        Range("CK" & target.Row) = Range("CK" & target.Row) + target
    End If
    End Sub

  3. #3
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: VBA to add cell contents to another cell

    Quote Originally Posted by yudlugar View Post
    in the worksheet object:
    ....
    Thanks for the quick reply, this worked once, but wouldn't work again. Any ideas?

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA to add cell contents to another cell

    Nope. Do you have more code in your workbook, if it worked once then I can only think maybe enableevents has been set to false?

    I might be able to give a better answer if you post a workbook.

  5. #5
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: VBA to add cell contents to another cell

    Quote Originally Posted by yudlugar View Post
    Nope. Do you h.....
    There is more code; here you go;

    Private Sub Worksheet_Activate()
    Application.Run Database!CalcMostRecentBooking
    End Sub
    
    
    Private Sub worksheet_change(ByVal target As Range)
    
    If Not Intersect(target, Range("T:T")) Is Nothing Then
    Range("CK" & target.Row) = Range("CK" & target.Row) + target
    End If
    
    Dim lngCol As Long
    
    
    If target.Column > 1 And target.Column < 7 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 95)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    
    If target.Column > 17 And target.Column < 24 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 96)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    
    If target.Column > 30 And target.Column < 38 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 97)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    
    If target.Column > 43 And target.Column < 51 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 98)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    
    If target.Column > 56 And target.Column < 64 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 99)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    
    If target.Column > 69 And target.Column < 77 Then
    ActiveSheet.Unprotect
    Application.EnableEvents = False
    With Cells(target.Row, Column + 100)
    .Value = Environ("Username") & " " & Now
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
    
    End With
    Application.EnableEvents = True
    End If
    End Sub
    I might have put it in the wrong place?

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA to add cell contents to another cell

    no it is fine in that place. I'd guess you had an error or something and enableevents got set to false. Try running a macro like:
    sub macro_1()
    application.enableevents = true
    end sub
    and then see if it starts working again

  7. #7
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: VBA to add cell contents to another cell

    Quote Originally Posted by yudlugar View Post
    no it is fine in that place. I'd guess you had an error or something and enableevents got set to false. Try running a macro like:
    I've figured out what was wrong - that cell is protected, so protection was getting in the way. I've rectified it now by adding a remove protection/reactivate protection code to it.

    Thanks very much!

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA to add cell contents to another cell

    In a blank macro, put it wherever. Just need to run it once then delete it. Alternatively close excel completely then open it again and see if it works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Validating contents of a cell on input based on the contents of another cell
    By Saigonet in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-03-2012, 03:07 AM
  2. Replies: 4
    Last Post: 10-01-2012, 11:15 AM
  3. [SOLVED] Move contents of a cell based on contents of another cell
    By djfscouse in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-06-2012, 10:31 AM
  4. Split cell contents over multiple rows based on cell contents
    By naigy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-17-2011, 05:38 PM
  5. How do I make a cell's contents equal to another cell's contents with macro program?
    By mgmcdevitt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2005, 04:44 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1