vb.net 在excel中找到最后一行并使用excel.range
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13169077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
finding last row in excel and using excel.range
提问by Jason Bayldon
I am attempting to use a reference to interop.excel instead of vba to do some worksheet formatting. My code looks like:
我正在尝试使用对 interop.excel 的引用而不是 vba 来进行一些工作表格式设置。我的代码看起来像:
Dim lastrow As Range = excel.Rows.End(XlDirection.xlDown)
Dim findme As Range = excel.Range("A1:A" & lastrow)
the excel.range does not allow me to use the "&" symbol here. How should I identify my last row of data?
excel.range 不允许我在这里使用“&”符号。我应该如何识别我的最后一行数据?
回答by J.Hudler
Looking at the Range Class Reference, it seems you just missed to put the Rowproperty of the lastrowRange:
查看Range Class Reference,您似乎只是错过了放置Range的Row属性lastrow:
Dim findme As Range = excel.Range("A1:A" & lastrow.row)
Dim findme As Range = excel.Range("A1:A" & lastrow.row)
回答by Ruben
I know this is an old thread but there are a lot of google references with "bad solutions", I think this is the an elegant solution to get the lasCell
我知道这是一个旧线程,但有很多谷歌参考“糟糕的解决方案”,我认为这是获得 lasCell 的优雅解决方案
set lastCell=yourSheet.Range("A1").SpecialCells(xlCellTypeLastCell)
so your code could be:
所以你的代码可能是:
Dim lastrow As Range = excel.Range("A1").SpecialCells(xlCellTypeLastCell).Row
Dim findme As Range = excel.Range("A1:A" & lastrow)
回答by Kiwi
lastrow is defined as a Range and you are trying to do a string concatenation between a string and a Range.
lastrow 被定义为一个范围,您正在尝试在字符串和范围之间进行字符串连接。
回答by user2684010
It looks at "A". But beware, if there is a merged cell going vertical, it takes the top cell of the merger. So if there is a merged cell going from A15 to A20 which is the last cell of the column, it will then classify A15 as the last cell even though its A20. I find that Neolisk's solution is the best in a case like that.
它看着“A”。但要注意,如果合并的单元格垂直,它会占用合并的顶部单元格。因此,如果有一个从 A15 到 A20 的合并单元格,这是该列的最后一个单元格,它会将 A15 归类为最后一个单元格,即使它是 A20。我发现在这种情况下,Neolisk 的解决方案是最好的。
回答by Ajit Singh
@Jason Bayldon It ends at last row where there is an entry. It skips blanks also. I am using VB.NET 2010. Here is the sample code.
@Jason Bayldon 它在有条目的最后一行结束。它也跳过空白。我正在使用 VB.NET 2010。这是示例代码。
Dim r2 As Excel.Range
Dim lastrow As Integer
r2 = oSheet.Range("Task_ID") 'Task_ID is my column name
lastrow = r2.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row 'returns last row

