【发布时间】:2023-03-30 02:26:01
【问题描述】:
在我的 VSTO 加载项项目中,如果我选择一个空单元格(如 F11)并在该范围内运行 find 以获取任何值,它似乎会搜索整个工作表并在我期望的时候返回范围 A1:E10返回空值。下面是我选择的单元格的图像。
我通过选择访问这个范围,当我在调试时检查时,它有 6 列和 11 行,这是我选择的。当我试图在这个范围内找到任何东西时,我希望我应该得到一个空值,但我得到的是 E10 的值,这是没有意义的。
这是我的 c# 代码,第一部分获取一个选择并遍历 范围区域,在单个选择的情况下只有 1 个区域。
即使 F11 是唯一选择的单元格,最后的 val1 和 val2 的值都是 3197077000。
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 时得到一个 null
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
【问题讨论】:
-
您在问题中添加了很多代码;其中很多是指我们大多数人没有或不知道它们是什么的图书馆。您能否创建一个minimal reproducible example,清楚地反映问题情况 - 仅使用标准 VSTO - 以便我们可以有效地调查?
-
@CindyMeister 更新为在一个较小的示例中使用标准 VSTO 库。