本文介绍了Excel VBA:迭代范围参数和更改单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我相信我想做的是很简单。我想迭代一个Range参数,并更改该范围内每个单元格的值。函数测试(thisRange As Range)
对于每个c在thisRange.Cells
c.Value = 1
下一个
结束函数
以上是我想要做的一个简单的例子,但似乎不起作用。当我调试这个时,Excel似乎在砸$ c.Value = 1 时出错。为什么这不起作用?
解决方案
这适用于我
Option Explicit
Sub Sample()
Dim ret
ret = test(Sheets(Sheet1)。Range(A1:A15 ))
End Sub
函数测试(thisRange As Range)
Dim c As Range
对于每个c在thisRange.Cells
c.Value = 1
下一个
结束功能
BTW我们不需要使用一个函数。一个函数用于返回值。尝试这个
Option Explicit
子样本()
测试表(Sheet1 ).Range(A1:A15)
End Sub
子测试(thisRange As Range)
Dim c As Range
对于每个c在thisRange.Cells
c.Value = 1
下一个
End Sub
I believe what I am trying to do is pretty simple. I want to iterate over a Range parameter and change the value for each cell in that range.
Function test(thisRange As Range) For Each c In thisRange.Cells c.Value = 1 Next End Function
The above is a simple example of what I want to do, but doesn't seem to work. When I debug this, Excel seems to be throwing an error when it hits c.Value = 1. Why does this not work?
解决方案
This 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
BTW we don't need to use a Function. A function is used to return a value. Try this
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
这篇关于Excel VBA:迭代范围参数和更改单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!