Search code examples
vbaexcelcellselected

Run a Macro through select cells


I need this macro to apply "Pass" to every individual cell in my selection. At the moment, even if I select multiple Cells, it only writes Pass on the first one that I selected.

I'm pretty new to this whole codding thing so I know I did something wrong.

Dim rngMyRange As Range
Dim cell As Range

Set rngMyRange = Selection
For Each cell In rngMyRange.Cells
ActiveCell = "Pass"
Next cell 

End Sub

Solution

  • When using a For Each loop. The variable, in this case cell represents each cell as it iterates. So changing the loop to this:

    For Each cell In rngMyRange.Cells
        cell = "Pass"
    Next cell
    

    should do the trick.

    ActiveCell only refers to the actual active cell and the loop does not change which cell is active. It is considered bad form to activate a cell in a loop, as it slows down the code.