Excel VBA: changing a range parameter value and changing cell values

I believe that what I'm trying to do is pretty simple. I want to iterate over the Range parameter and change the value for each cell in this range.

Function test(thisRange As Range) For Each c In thisRange.Cells c.Value = 1 Next End Function 

The above is an example of what I want to do, but does not seem to work. When I debug this, Excel seems to throw an error when it reaches c.Value = 1 . Why is this not working?

+6
source share
1 answer

It works for me

 Option Explicit Sub Sample() Dim ret ret = test(Sheets("Sheet1").Range("A1:A15")) End Sub Function test(thisRange As Range) Dim c As Range For Each c In thisRange.Cells c.Value = 1 Next End Function 

By the way, we do not need to use the function. The function returns a value. try it

 Option Explicit Sub Sample() test Sheets("Sheet1").Range("A1:A15") End Sub Sub test(thisRange As Range) Dim c As Range For Each c In thisRange.Cells c.Value = 1 Next End Sub 
+3
source

Source: https://habr.com/ru/post/926984/


All Articles