【问题标题】:How do I get an Excel cell name in vb.net?如何在 vb.net 中获取 Excel 单元格名称?
【发布时间】:2016-10-26 05:23:15
【问题描述】:

我需要列中第一个空单元格的名称,例如“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

【问题讨论】:

标签: vb.net excel visual-studio worksheet


【解决方案1】:

要获取列的名称,请使用:

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

问候, 亚科夫

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多