Results 1 to 9 of 9

Thread: [RESOLVED] [Excel] Add (and remove) cells to a range

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Resolved [RESOLVED] [Excel] Add (and remove) cells to a range

    I have a For Each loop running on the Worksheet_Change event. The loop runs through each cell in the target range, and picks out those cells that actually have changed. Once it finds a cell, I want it to add it to a global variable range.

    Consider the code:
    Code:
    Dim lastTarget(0 To 50) As String
    Dim changedCells as Range
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cell As Range
        Dim j As Integer
        
        For Each cell In Target        
            'Check to see if the cell actually changed
            If lastTarget(j) <> cell.Value Then             
                    'Add changed cell to a list
                    Set changedCells = Union(changedCells, cell)                  
            End If
            j = j + 1            
        Next cell
    End Sub
    Running this code gives me a "Run-time error '5': Invalid procedure call or argument" when it tries:
    Code:
          'Add changed cell to a list
          Set changedCells = Union(changedCells, cell)
    I'm assuming it's giving me an error because of the Union() call, but I'm passing 2 ranges as it expects. I would just pass the cell.Address to a string array, but I need the changedCells to be a range so I can use it in an Intersect() later. Anyone able to help?

    ***This problem has been solved, "Both parameters for Union() must NOT be Nothing."
    ***New issue: removing a cell from a range, see below.
    Last edited by Zurm; Sep 29th, 2010 at 04:07 PM. Reason: Updated

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [Excel] Add cells to a range

    i would guess from looking the problem is that changedcells doesn't have a value to start with. This would be an invalid argument. No value = no range.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [Excel] Add cells to a range

    Both parameters for Union() must NOT be Nothing.
    Code:
    If changedCells Is Nothing then
        Set changedCells = cell
    Else
        Set changedCells = Union(changedCells, cell)
    End If
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: [Excel] Add cells to a range

    Alright, well that fixed it. Now here's one that I think will be a little more challenging.

    Now I need to be able to remove cells form a range.

    Here's what I am trying to accomplish so you guys can see better:
    I'm attempting to create an advanced undo function. Every time a cell is changed, it generates an SQL statement listed on another sheet. What I want to be able to do, is add an "Undo change to this cell" function to the right click event when it intersects a cell in changedCells. Once the UndoChange function is run on the target cell, I need to remove that target cell from the changedCells range, and delete the SQL statement it generated for that change.

    Any ideas?

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: [Excel] Add (and remove) cells to a range

    Well I had one idea, and that was to add the cells I want removed from changedCells to a new range. Then only act if the cell intersected changedCells, and not the other range. Works ok, not sure if there's a more efficient way to go about it though.

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [Excel] Add (and remove) cells to a range

    i'm not sure if you can work directly in excel from your external program, but the vba in excel has access to cell properties such as tag. You could store the undelete info in the tag.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: [Excel] Add (and remove) cells to a range

    I'm working with VBA directly in excel. I have a place to store the old value of the cell, my issue is keeping track of which cells changed.

    I have a list of the cell addresses, but I need to add and remove cells from a range on the fly.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Add (and remove) cells to a range

    to remove a cell from the range, i believe you would need to loop through all the cells in the range and create a temp range, minus the deleted cell, then assign the temp range back to the original range, this may be slow, depending on the size of the range
    something like
    vb Code:
    1. Dim r As Range, tmp As Range
    2. deletedcelladdr = "$A$4"
    3. Set r = Range("a1:a6")
    4. Set r = Union(r, Range("d3"))
    5. For Each c In r
    6.     If Not c.Address = deletedcelladdr Then If Not tmp Is Nothing Then Set tmp = Union(tmp, c) Else Set tmp = c
    7. Next
    8. Set r = tmp
    appears to work correctly
    ?r.Address
    $A$1:$A$3,$A$5:$A$6,$D$3
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: [Excel] Add (and remove) cells to a range

    Worked perfectly.

    Thanks very much everyone. You guys are great.

Posting Permissions

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



Click Here to Expand Forum to Full Width