Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove cell from Range (object)

Background
My code does some loops over ranges, however, each interaction should be performed in the range excluding the cell just performed. I think the easier way to do so is to remove the cell from the stored range.
Problem
I have not been able to find a way to remove cell from the stored object
Code
The question is general but, for the matters it would be something like

Sub Sample()
Dim RangeToAnalyze As Range
Dim CounterRange As Long
Dim ExcludeCell As Range 'sample on what is desired to achieve
    Set RangeToAnalyze = Selection 'this is based on some other criteria but, in order to reproduce it easier that's why selection
    For CounterRange = 1 To 5
    Set ExcludeCell = RangeToAnalyze.Find("text")
    'now here I would like to find the next cell, but it should exclude the first one in order to go to the next one
    Set RangeToAnalyze = RangeToAnalyze.Exclude(ExcludeCell) 'this is what I want to do, so when looping it could jump to the next find (This function is "sample" this is what I am looking to do
    Next CounterRange
End Sub
like image 941
Sgdva Avatar asked Aug 23 '16 14:08

Sgdva


People also ask

How do I exclude a cell from a range?

If cells you need to exclude from the summing locating in discontinuous cells, for example, you need to exclude values in cell A3 and A6 while summing range A2:A7, please apply this formula: =SUM(A2:A7)-A3-A6.

How do I remove a range of cells in Excel?

On the Home tab, in the Cells group, click the arrow next to Delete, and then click Delete Cells.

How do I exclude cells in a macro?

Then run the macro. You are asked to choose the cells to be unselected. You can do so by simply selecting the cells with the mouse, holding down the Shift key as you click on each one. When you dismiss the input box, the selection you started with is modified to exclude the cells you selected.


2 Answers

One approach could be this

Function getExcluded(ByVal rngMain As Range, rngExc As Range) As Range

    Dim rngTemp     As Range
    Dim rng         As Range

    Set rngTemp = rngMain

    Set rngMain = Nothing

    For Each rng In rngTemp
        If rng.Address <> rngExc.Address Then
            If rngMain Is Nothing Then
                Set rngMain = rng
            Else
                Set rngMain = Union(rngMain, rng)
            End If
        End If
    Next

    Set getExcluded = rngMain



End Function

Test the function

Sub test()

    MsgBox getExcluded(Range("A1:M10000"), Range("a10")).Address

End Sub
like image 116
cyboashu Avatar answered Sep 28 '22 22:09

cyboashu


You would be better to use a For... Each loop I suspect. This should be a starting place:

Sub Sample()

    Dim RangeToAnalyze As Range
    Dim rngCell as Range

    Set RangeToAnalyze = Range("Selection")

    For each rngCell in RangeToAnalyze
        'Your other code/actions here
    Next rngCell

    'more code here

End Sub

This should then perform your actions on each cell and move on to the next one and automatically stop at the last cell.

You can also nest this inside another For... Each loop to cycle different ranges as well and so on.

like image 35
Matt_Roberts Avatar answered Sep 28 '22 21:09

Matt_Roberts