0

I have written a program that finds a cell containing "Customer:" and the program works successfully. The problem is, I want the value of the cell directly next to the cell. The layout looks something like this:

__________   _________
|Customer:| | Steve  |
|_________| |________|
|Customer:| | John   |
|_________| |________|
|Customer:| | Frank  |
|_________| |________|

So in this case I would want the values "Steve", "John", and "Frank". How can I do this?

Thanks, Luke

My Code:

public void gatherInfo()
    {
        string temp = "";

        Excel.Range currentFind = null;
        Excel.Range firstFind = null;

foreach (Excel.Worksheet sheet in excelWorkbook.Application.Worksheets)
        {
            try
            {
                // access cell within sheet
                Excel.Range excelCell =
                      (Excel.Range)sheet.get_Range("A1", Type.Missing);                   

                currentFind = excelCell.Find("Customer:", Type.Missing,
            Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
            Type.Missing, Type.Missing);

                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))
                    {
                        //String value of cell
                        temp = currentFind.Value2.ToString();

                        break;
                    }

                    currentFind = excelCell.FindNext(currentFind);

                }

                //Find adjacent cell value here?

                holdInformation.Add(temp);

            }

            catch
            {
                Console.WriteLine("Couldn't get customer name");
            }
Luke R
  • 97
  • 1
  • 1
  • 14

1 Answers1

1

i think the offset function is what you are looking for. On your code, you could add a line that goes:

firstFind.Offset[0, 1]; 

"0" to signify that you target the current row "1" to signify that you want to target the 1st column on the right from the excel range

suisgrand
  • 110
  • 1
  • 5
  • Thanks! That worked. The only thing is... I just realized that the single cell spans from C to H. Will it still read the value if I just reference the part in C? – Luke R Oct 25 '16 at 14:48
  • *Columns C to H Like so: C D E F G H – Luke R Oct 25 '16 at 14:49
  • I don't know much about C#. But maybe the `Resize` property works, also? – Brian Oct 25 '16 at 14:59
  • Okay. I will try that. @Brian – Luke R Oct 25 '16 at 15:00
  • Would it be something like this? int cols = firstFind.Columns.Count; int row = firstFind.Rows.Count; currentFind = firstFind.Offset[0, 1]; currentFind.get_Resize(row, cols + 5); – Luke R Oct 25 '16 at 15:09
  • Thanks everyone for your assistance. I realized that I was over-complicating my issue. I was able to find a solution here: http://stackoverflow.com/a/22718076/5970326 – Luke R Oct 25 '16 at 17:29