Results 1 to 9 of 9

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

Threaded View

  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

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