Range类的FindNext方法失败(C#,Excel)

时间:2015-08-19 20:39:00

标签: 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();
        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方法失败了)..我一直在使用它作为参考(https://msdn.microsoft.com/en-us/library/e4x1k99a.aspx)并且不了解可能出错的地方。< / p>

由于

2 个答案:

答案 0 :(得分:1)

这对我来说相当令人沮丧,但只是改变:

// Pair.h
// template for a pair of items of arbitrary types

#include <sstream>
using namespace std;

template <typename S, typename T>
class Pair
   {
      public:
               Pair();
               Pair(S,T);
               Pair(Pair &);
               ~Pair();
               const Pair &operator=(const Pair &other);

               string toString();

               S* getFirst();
               T* getSecond();
              int getPairCount();

               void setFirst(S);
               void setSecond(T);

      private:
               S *f;
               T *s;
               static int count;

   };

template <typename S, typename T>
int Pair<S,T>::count=0;

// 0-parameter constructor
template <typename S, typename T>
Pair<S,T>::Pair()
   {
      f = NULL;
      s = NULL;
      count++;
   }

// 2-param constructor
template <typename S, typename T>
Pair<S,T>::Pair(S x, T y)
   {
      f = new S;  *f = x;
      s = new T;  *s = y;
      count++;
   }


// get first element in pointer
template <typename S, typename T>
S* Pair<S,T>::getFirst()
   {
      if (f!=NULL)
         {  S *tmp = new S;
            *tmp = *f;
            return tmp;
         }
      else
         return NULL;
   }

// get second element in pointer
template <typename S, typename T>
T* Pair<S,T>::getSecond()
   {
      if (s!=NULL)
         {
            T *tmp = new T;
            *tmp = *s;
            return tmp;
         }
      else
         return NULL;
   }

// set first element
template <typename S, typename T>
void Pair<S,T>::setFirst(S x)
   {
      if (f==NULL)
         f = new S;
      *f = x;
   }

// set second element
template <typename S, typename T>
void Pair<S,T>::setSecond(T y)
   {
      if (s==NULL)
         s = new T;
      *s = y;
   }

// make a string representation
template <typename S, typename T>
string Pair<S,T>::toString()
   {
      stringstream ss;
      ss<<"(";
      if (f==NULL)
         ss<<"NULL";
      else
         ss<<(*f);
      ss<<",";
      if (s==NULL)
         ss<<"NULL";
      else
         ss<<(*s);
      ss<<")";
      return ss.str();
   }

// keep count
template <typename S, typename T>
int Pair<S,T>::getPairCount(){
    return count;}

//copy constructor
template <typename S, typename T>
Pair<S,T>::Pair(Pair &other)
{
f = NULL; s = NULL;
    if(other.f != NULL)
    f = new S(*other.f);

    if(other.s != NULL)
    s = new T(*other.s);

    count++;
}
//destructor

template <typename S, typename T>
Pair<S,T>::~Pair()
{
    if(f != NULL)
    delete f;

    if(s != NULL)
    delete s;

    f = NULL;
    s = NULL;

    count--;
}

//deep assignment

template <typename S, typename T>
const Pair<S,T> & Pair<S,T>::operator=(const Pair<S,T> &other)
{
    if(this != &other)
    {
        if(f != NULL)
        delete f;

        if(s != NULL)
        delete s;

        f = NULL; s = NULL;

        if(other.f != NULL)
        f = new S(*other.f);

        if(other.s != NULL)
        s = new T(*other.s);
    }
    return *this;
}

为:

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;
}

我不知道为什么......但它确实存在。我希望这有帮助!

答案 1 :(得分:0)

我遇到了同样的问题。就我而言,我在另一个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);

    }

}

也请参见以下链接:https://www.pcreview.co.uk/threads/why-is-my-findnext-not-working.4026685/