Saturday, 19 August 2017

Excel VBA - Performing function to each cells in range

It's very easy, but the final code will depend on where you want to store the new values. For example, if you want to store the values divided by 2 in the next column:



Sub test()


Dim cell As Range

For Each cell In Range("A1:A5")
cell.Offset(, 1).Value = cell.Value / 2
Next

End Sub


Mind you there are more efficient ways to do this than using offset if your range is large, but for a smaller range, this is totally acceptable and fast.




If you want to overwrite the values, you can simply use cell.Value in place of cell.Offset(,1).Value

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...