Set range then add values to existing values
I have code the defines a dynamic range then for all cells in that range I want to add the same value but I do NOT want to overwrite the existing numbers. The following code is similar to what I currently have except the range is not dynamic as this is just an example:
Code:
Sub test()
Dim rng As Range
Dim mynumber As Integer
mynumber = 10
Set rng = Range(Cells(1, 1), Cells(10, 1))
rng.value = mynumber
End Sub
Obviously this just makes all cells in range rng equal to 10 (overwrites existing values). I also tried:
Code:
Sub test()
Dim rng As Range
Dim mynumber As Integer
mynumber = 10
Set rng = Range(Cells(1, 1), Cells(10, 1))
rng = rng + mynumber
End Sub
But just get a "type mismatch" error. I was playing around with some way of copying the existing numbers in the range, pastespecial values somewhere else, then do rng = mynumber then copy/pastespecial add values the original numbers back but this seems a very messy way of doing things. There must be a simple way of just adding to the existing values that I am missing!
Thanks
-Rob
Re: Set range then add values to existing values
you can do a for each cell in the range
Code:
For Each c In rng
c.Value = c + mynumber
Next