vba 通过 VB.net 使用 Excel 的“单元格”选择连续范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18280142/
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
Selecting contiguous ranges using "cells" for Excel through VB.net
提问by Zabalba
I can't seem to find a way to use one of my VBA tricks in VB.net
我似乎找不到在 VB.net 中使用我的 VBA 技巧之一的方法
For example selecting this range works in VBA:
例如,在 VBA 中选择此范围有效:
Dim Border As Range
Border = Range(Cells(2, 3), Cells(10, 4)).Select
In Visual Studio I can only use one "Cells" to call a range. The same code above underlines the word "Cells" saying its not declared even though I have:
在 Visual Studio 中,我只能使用一个“单元格”来调用一个范围。上面相同的代码强调了“细胞”这个词,说它没有声明,即使我有:
Dim Border As Excel.Range
Border = Range(Cells(2, 3), Cells(10, 4)).Select()
I use .Row and .Column to identify the data-sets dimensions. I tried looking everywhere for an explanation/solution. Maybe I'm looking to hard in the wrong direction. Anyone have any idea?
我使用 .Row 和 .Column 来标识数据集维度。我尝试到处寻找解释/解决方案。也许我正朝着错误的方向努力。任何人有任何想法?
采纳答案by Cor_Blimey
You need to qualify Cells with the Range or Worksheet. Cells reference is not implied as it is in VBA.
您需要使用范围或工作表限定单元格。单元格引用并不像在 VBA 中那样隐含。
C#:
C#:
internal static void TestRangeCells(Worksheet worksheet)
{
Range range = worksheet.Range(worksheet.Cells[1, 1], worksheet.Cells[5, 10]);
MessageBox.Show(range.Address);
}
回答by FrankPl
According to http://msdn.microsoft.com/de-de/library/aa288993(v=vs.71).aspx, the following should work:
根据http://msdn.microsoft.com/de-de/library/aa288993(v=vs.71).aspx,以下应该有效:
Border = ws.get_Range("B3:J4",Type.Missing);
This just uses the cell names with columns coded as letters instead of the indexes (2 -> B, 10 ->J). You probably can build the string dynamicilly in code if you need to.
这只是使用带有编码为字母的列的单元格名称而不是索引 (2 -> B, 10 ->J)。如果需要,您可能可以在代码中动态构建字符串。