Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用索引查找具有值的最后一个单元格_Vb.net_Excel_Excel Interop_Vba - Fatal编程技术网

Vb.net 使用索引查找具有值的最后一个单元格

Vb.net 使用索引查找具有值的最后一个单元格,vb.net,excel,excel-interop,vba,Vb.net,Excel,Excel Interop,Vba,我尝试在包含值的列中查找最后一个单元格。我知道我可以使用类似于: Dim findme As Range = excel.Range("A1:A" & lastrow.row) 但我正在寻找一种不使用此列字符格式的方法。类似于VBA格式的东西 Dim rng as range with myWorksheet LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row rng = .Range(.Cells(1,1), .Cells(L

我尝试在包含值的列中查找最后一个单元格。我知道我可以使用类似于:

Dim findme As Range = excel.Range("A1:A" & lastrow.row)
但我正在寻找一种不使用此列字符格式的方法。类似于VBA格式的东西

Dim rng as range
with myWorksheet
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    rng = .Range(.Cells(1,1), .Cells(LastRow, 1))
end with

我创建了一个帮助器类来解决此问题:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class FindLastCellByIndex
    Private Shared Function getStringRangeFormat(ByVal colNumber As Long, ByVal rowNumber As Long) As String
        If colNumber > 0 AndAlso colNumber < 27 Then
            Dim c As Char
            c = Convert.ToChar(colNumber + 64)
            Return (c & rowNumber & ":" & c).ToString
        Else
            Return ""
        End If
    End Function

    Public Shared Function getSearchRange(ByRef _sheet As Excel.Worksheet, ByVal Col As Long, ByVal startRow As Long) As Excel.Range
        Dim strColCharSpez As String = getStringRangeFormat(Col, startRow)
       'Build String
        Dim rng As Excel.Range = DirectCast(_sheet.Cells(_sheet.Rows.Count, startRow), Excel.Range)
        Dim t As Long
        t = rng.End(Excel.XlDirection.xlUp).Row

        Return _sheet.Range(strColCharSpez & t)

    End Function
End Class

现在您可以将范围:
Range(Cells(3,3)
Cells(LastRow,3)
作为一个对象。

就是这样做的。请记住,如果
.Cells(.Rows.Count,1)
有一个值,那么在使用
End
函数时,您将不会得到最后使用的单元格。仍然奇怪,我一直认为:“如果我可以在VBA中完成,我也可以通过互操作在VB.NET中完成。”这显然是错误的。
Dim rng As Excel.Range = FindLastCellByIndex.getSearchRange(_sheet, 3, 3)