如何在vb.net?

时间:2016-06-23 18:17:42

标签: vb.net excel visual-studio worksheet

我需要列中第一个空单元格的名称,例如“E15”或“A3” 我尝试过使用worksheet.Cells.Name和worksheet.Rows.Name,但我没有 认为这是正确的语法...请帮助!

这是我的代码

 Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(eXe)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")



    Dim eColumn As Excel.Range = xlWorkSheet.Range("E2:E15")
    Dim rCell As Excel.Range




    For Each rCell In eColumn
        If rCell Is Nothing Then
            Dim LastCell As String = xlWorkSheet.Rows.Name
            MsgBox(LastCell)
        End If
        MsgBox(rCell.Value)
    Next rCell

(更新)

我使用了以下代码并得到了$ E $ 15,有没有办法用“$”符号获取地址?

For Each rCell In eColumn
        If rCell.Value = "" Then
            Dim LastCell As String = rCell.Cells.Address
            MsgBox(LastCell)
        End If
        MsgBox(rCell.Value)
    Next rCell

1 个答案:

答案 0 :(得分:1)

要获取coloumn使用的名称:

Dim columnLetter As String = ColumnIndexToColumnLetter(85) ' returns CG
Private Function ColumnIndexToColumnLetter(colIndex As Integer) As String
    Dim div As Integer = colIndex
    Dim colLetter As String = String.Empty
    Dim modnum As Integer = 0

    While div > 0
        modnum = (div - 1) Mod 26
        colLetter = Chr(65 + modnum) & colLetter
        div = CInt((div - modnum) \ 26)
    End While

    Return colLetter
End Function

你已经知道如何获得你想要的行号了。然后使用

dim CellName as String = columnLetter & rownum ' the row number you need

这应该省略你不想要的任何$标志

该示例来自here

此致 Iakov