VB.Net:从行和列获取单元格地址

时间:2017-01-04 16:38:48

标签: excel vb.net for-loop range cell

我知道之前已经问过这个问题,但我已经尝试过对这两个问题的答案,但对我来说都没有。

当前代码:

Dim aString As String
Dim address As String
For Each thisTriangle In triangleArray
    drawTriangle(rowToDrawIn, thisTriangle.getMarkedCell)
    aString = String.Format("Activity Name: {0}", thisTriangle.getActivityDescription)

    //here is where I am trying to get the address
    address = xlWorkSheet.Cells.Address(rowToDrawIn, thisTriangle.getMarkedCell)
    xlWorkSheet.Range(address).AddComment(aString)
Next

如您所见,我试图获取地址,然后在该地址发表评论。运行此代码时,我收到运行时错误。 thisTriangle.getMarkedCell返回一个int作为列号,rowToDrawIn是行号。

非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:1)

假设rowToDrawInthisTriangle.getMarkedCell都是正数。

你的错是Cells.Address。如果你通过Cells.Address(1,2),你希望获得$B$1,但实际上你得到$1:$10485761,现在你正在尝试为这个范围添加注释,因此参数错误。

Cells.Address(1,2)!= Cells(1,2).Address

更改此

  address = xlWorkSheet.Cells.Address(rowToDrawIn, thisTriangle.getMarkedCell)
    xlWorkSheet.Range(address).AddComment(aString)

xlWorkSheet.Cells.(rowToDrawIn, thisTriangle.getMarkedCell).Comment.Delete
xlWorkSheet.Cells.(rowToDrawIn, thisTriangle.getMarkedCell).AddComment(aString)