Excel vba:键入范围和单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9455738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel vba : Type range and cell
提问by bsr
I am bit confused by cell and range in vba (excel). Logically, i could think a cell as a range with size = 1; and I think it is easy to make a range out of a cell.
我对 vba (excel) 中的单元格和范围有点困惑。从逻辑上讲,我可以将单元格视为大小为 1 的范围;我认为从单元格中创建一个范围很容易。
If I read the api of EntireRow property hereit operates on range. But, the below code work, indicating 'cell'variable inside the loop is a range
如果我在这里阅读 EntireRow 属性的 api, 它会在范围内运行。但是,下面的代码工作,表明循环内的“单元格”变量是一个范围
Set import = Sheets("import")
Set spRange = import.Range("A2")
Set spRange = import.Range("A2:" & spRange.End(xlDown).Address)
For Each cell In spRange
dict.Add cell.Offset(0, 2).Text, cell.EntireRow
Next cell
At the same time, the below code returns an error indicating type mismatch when call the removecellfunction. What should be the type of targetCellin the function definition?
同时,下面的代码在调用removecell函数时返回一个错误指示类型不匹配。函数定义中targetCell的类型应该是什么?
Set spRange = mySheet.Range("b2", mySheet.Range("b2").End(xlDown))
For Each cell In spRange
val = removecell (cell)
Next cell
Public Function removecell(targCell As Range) As Boolean
removecell = False
End Function
采纳答案by Doug Glancy
This compiles and runs:
这将编译并运行:
Sub Tester()
Dim spRange As Excel.Range
Dim cell As Excel.Range
Dim mySheet As Excel.Worksheet
Dim val As Boolean
Set mySheet = ActiveSheet
Set spRange = mySheet.Range("b2", mySheet.Range("b2").End(xlDown))
For Each cell In spRange
val = removecell(cell)
Next cell
End Sub
Public Function removecell(targCell As Range) As Boolean
removecell = False
End Function

