在我的VSTO插件项目中,如果我选择一个单一的空单元格,比如F11,然后在该范围内运行查找,寻找任何值,它似乎搜索整个工作表,并返回A1:E10范围,而我希望它返回空值。下面是我选择的单元格的图像。
我是通过选择来访问这个范围的,当我在调试时检查时,它的列数为6,行数为11,这就是我选择的内容。当我试图在这个范围内找到任何东西时,我希望我应该得到一个null,但我得到的却是E10的值,这毫无意义。
下面是我的c#代码,第一部分得到一个选择,然后遍历范围内的区域,在单个选择的情况下,只有一个区域。
最后val1和val2的值都是3197077000,尽管F11是唯一被选中的单元格。
using Microsoft.Office.Interop.Excel;
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Excel.Range selection = Globals.ThisAddIn.Application.Selection;
if (null != selection)
{
var sheet = selection.Worksheet;
var book = (Excel.Workbook)sheet.Parent;
var areasCount = selection.Areas.Count;
for (int i = 1; i <= areasCount; i++)
{
var area = selection.Areas[i];
var test = area.Value;
var testCount = area.Count;
var testRow = area.Row;
var testCol = area.Column;
Excel.Range lastCellInLastRow = area.Find(
"*",
Type.Missing,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByRows,
XlSearchDirection.xlPrevious,
false,
Type.Missing,
Type.Missing);
Excel.Range lastCellInLastCol = area.Find(
"*",
Type.Missing,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByColumns,
XlSearchDirection.xlPrevious,
false,
Type.Missing,
Type.Missing);
var val1 = lastCellInLastCol.Value;
var val2 = lastCellInLastRow.Value;
}
}
}
}
}
当我在VBA中做同样的事情时,当在F11上运行find时,我得到了一个空值,正如我所期望的那样。
Option Explicit
Public Sub SelectionTest()
Dim selection As Variant
Dim rng As Range
Dim UsedRange As Variant
Dim lastCell As Range
Set selection = Application.selection
Dim area As Range
Debug.Print "selection columns " & selection.Columns.Count
Dim finalRng As Range
Dim finalArr As Variant
For Each area In selection.Areas
Debug.Print "single rng columns " & area.Columns.Count
Set lastCell = area.Find(What:="*", After:=area.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
Set lastCell = area.Find(What:="*", After:=area.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Debug.Print lastCell
Next
End Sub
简答。Interop Range.Find可能不可靠。尽量少用Interop。
更长的答案:Interop Range.Find可能不可靠,尽量少用Interop。我在实践中注意到的一个不靠谱的地方是: Range.Find
是它似乎对之前最后搜索的内容有记忆。这可能是也可能不是这里的问题,但如果我是你,我就不会费心去弄清楚它,因为你可以按以下方式解决你的问题。
C#
regex库和嵌套for循环来搜索数组。虽然多了点工作,但为了摆脱Interop这个不可靠的基础,还是值得的。这里有一个小的代码,让你开始,围绕读取范围。
public string[,] ReadValues(Range range)
{
return ConvertToStringArray2D(GetArray(range, () => range.Value2));
}
private string[,] ConvertToStringArray2D(Array values)
{
string[,] retArray = new string[values.GetLength(0), values.GetLength(1)];
// loop through the 2-D System.Array and populate the 1-D String Array
for (int row = 1; row <= values.GetLength(0); row++)
{
for (int col = 1; col <= values.GetLength(1); col++)
{
if (values.GetValue(row, col) == null)
{
retArray[row - 1, col - 1] = "";
}
else
{
retArray[row - 1, col - 1] = values.GetValue(row, col).ToString();
}
}
}
return retArray;
}
private Array OneBasedSingletonArray2D()
{
return Array.CreateInstance(typeof(Object), new int[] { 1, 1 }, new int[] { 1, 1 });
}
private Array GetArray(Range range, Func<object> dataProducer)
{
if (range.Cells.Count == 0)
{
return Array.CreateInstance(typeof(Object), 0, 0);
}
if (range.Cells.Count == 1)
{
//Creating a 1-based array to fit with Excel's 1-based indexing
Array retArray = OneBasedSingletonArray2D();
retArray.SetValue(dataProducer.Invoke(), 1, 1);
return retArray;
}
else
{
return (Array)dataProducer.Invoke();
}
}