Excel vba:键入范围和单元格

时间:2022-12-25 22:18:39

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)中的单元格和范围感到有点困惑。从逻辑上讲,我可以将单元格视为size = 1的范围;我认为从一个单元格中制作一个范围很容易。

If I read the api of EntireRow property here it operates on range. But, the below code work, indicating 'cell'variable inside the loop is a range

如果我在这里阅读了EntireRow属性的api,它就会在范围内运行。但是,下面的代码工作,表明循环内的'cell'变量是一个范围

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 removecell function. What should be the type of targetCell in 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

1 个解决方案

#1


4  

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

#1


4  

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