范围类的FindNext方法失败(C#,Excel)
我正在搜索包含以下项目的excel列: MT1325 MT0604 MU3509 MT0605 MT0606 MU3509 MT0607 MT0608 我希望以下代码输出消息框中以MT开头的每个项目:范围类的FindNext方法失败(C#,Excel),c#,excel,C#,Excel,我正在搜索包含以下项目的excel列: MT1325 MT0604 MU3509 MT0605 MT0606 MU3509 MT0607 MT0608 我希望以下代码输出消息框中以MT开头的每个项目: private void button1_Click(object sender, EventArgs e) { //New Excel App Excel._Application oApp = new Excel.Application();
private void button1_Click(object sender, EventArgs e)
{
//New Excel App
Excel._Application oApp = new Excel.Application();
oApp.Visible = true;
//Opens Workbook with MT/MU's to be counted
Excel.Workbook oWorkbook = oApp.Workbooks.Open("C:\\Users\\sfrey\\Desktop\\Test22");
Excel.Worksheet oWorksheet = oWorkbook.Worksheets["Sheet1"];
Excel.Range currentFind = null;
Excel.Range firstFind = null;
object misValue = System.Reflection.Missing.Value;
Excel.Range xlRange = oWorksheet.get_Range("A1");
currentFind = xlRange.EntireColumn.Find("MT",
misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext,
true, misValue, misValue);
while (currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
string cellText = currentFind.Value.ToString();
MessageBox.Show(cellText);
currentFind = xlRange.FindNext(currentFind);
}
}
最后一行代码给了我一个错误(Range类的FindNext方法失败)…我一直在使用它作为参考(),不知道会出什么问题
谢谢这让我相当沮丧,但只是改变了:
Excel.Range foundCell = nameColumnToSearch.Find(nameToFind);
Excel.Range firstResult = foundCell;
while (foundCell != null)
{
// Snip: Do some stuff
foundCell = nameColumnToSearch.FindNext(foundCell);
if (foundCell.Address == firstResult.Address)
foundCell = null;
}
致:
我不知道为什么。。。但它在那里。我希望这有帮助 这对我来说相当令人沮丧,但只是改变了:
Excel.Range foundCell = nameColumnToSearch.Find(nameToFind);
Excel.Range firstResult = foundCell;
while (foundCell != null)
{
// Snip: Do some stuff
foundCell = nameColumnToSearch.FindNext(foundCell);
if (foundCell.Address == firstResult.Address)
foundCell = null;
}
致:
我不知道为什么。。。但它在那里。我希望这有帮助 我也面临同样的问题。在我的例子中,我在另一个Find语句中运行Find,并按照Matthew Esler所做的相同方式进行了修复 改为使用FindNext,改为Find,并将After:参数设置为currentFind。这对我有用。我仍然不知道FindNext为什么会有这种行为
private void button1_Click(object sender, EventArgs e)
{
//New Excel App
Excel._Application oApp = new Excel.Application();
oApp.Visible = true;
//Opens Workbook with MT/MU's to be counted
Excel.Workbook oWorkbook = oApp.Workbooks.Open("C:\\Users\\sfrey\\Desktop\\Test22");
Excel.Worksheet oWorksheet = oWorkbook.Worksheets["Sheet1"];
Excel.Range currentFind = null;
Excel.Range firstFind = null;
object misValue = System.Reflection.Missing.Value;
Excel.Range xlRange = oWorksheet.get_Range("A1");
currentFind = xlRange.EntireColumn.Find("MT", misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, true, misValue, misValue);
while (currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) == firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
string cellText = currentFind.Value.ToString();
MessageBox.Show(cellText);
// currentFind = xlRange.FindNext(currentFind);
currentFind = xlRange.EntireColumn.Find("MT", currentFind, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, true, misValue, misValue);
}
}
也可以查看此链接:我遇到了同样的问题。在我的例子中,我在另一个Find语句中运行Find,并按照Matthew Esler所做的相同方式进行了修复 改为使用FindNext,改为Find,并将After:参数设置为currentFind。这对我有用。我仍然不知道FindNext为什么会有这种行为
private void button1_Click(object sender, EventArgs e)
{
//New Excel App
Excel._Application oApp = new Excel.Application();
oApp.Visible = true;
//Opens Workbook with MT/MU's to be counted
Excel.Workbook oWorkbook = oApp.Workbooks.Open("C:\\Users\\sfrey\\Desktop\\Test22");
Excel.Worksheet oWorksheet = oWorkbook.Worksheets["Sheet1"];
Excel.Range currentFind = null;
Excel.Range firstFind = null;
object misValue = System.Reflection.Missing.Value;
Excel.Range xlRange = oWorksheet.get_Range("A1");
currentFind = xlRange.EntireColumn.Find("MT", misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, true, misValue, misValue);
while (currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) == firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
string cellText = currentFind.Value.ToString();
MessageBox.Show(cellText);
// currentFind = xlRange.FindNext(currentFind);
currentFind = xlRange.EntireColumn.Find("MT", currentFind, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, true, misValue, misValue);
}
}
也可以查看此链接:链接示例将第三个到最后一个参数设置为false,您在这一行中将自己的参数设置为true
currentFind=xlRange.entireclumn.Find(“MT”,misValue,Excel.XlFindLookIn.xlValues,Excel.XlLookAt.xlPart,Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlNext,true,misValue,misValue)代码>不是这样,我相信真/假是是否匹配大小写。我确实试过了,但没有成功。他们的示例有以下Excel.Range Fruits=Application.get_Range(“A1”、“B3”)
但是您试图根据工作簿找到它,而且您有整个列,示例不在其中xlRange.EntireClumn.find
为什么不先尝试使用链接中的示例使其工作,然后再从那里扩展它,如果您需要做更多的工作..尝试确保find
和FindNext
(即定义xlRange
以包括entireclumn
部分)。链接示例将第三个到最后一个参数设置为false,您在这一行中将自己的参数设置为truecurrentFind=xlRange.entireclumn.Find(“MT”,misValue,Excel.XlFindLookIn.xlValues,Excel.XlLookAt.xlPart,Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlNext,true,misValue,misValue)代码>不是这样,我相信真/假是是否匹配大小写。我确实试过了,但没有成功。他们的示例有以下Excel.Range Fruits=Application.get_Range(“A1”、“B3”)
但是您试图根据工作簿找到它,而且您有整个列,示例不在其中xlRange.EntireClumn.find
为什么不先尝试使用链接中的示例使其工作,然后再从那里扩展它,如果您需要做更多的工作..尝试确保find
和FindNext
(即定义xlRange
以包括entireclumn
部分)。