简体   繁体   中英

How can I iterate over a single excel column, but have it stop at the last cell with data?

My variable "col", gets the data in column B. I want to search column B for certain data it contains, but not have it infinitely looping and searching forever.

 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open("X:\\Private\\DATA\\PROJECT DATA\\Database\\TETRA\\Master Captured List - TETRA.xlsx");
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)excelWorkbook.Sheets[sheetSpaces];
            Excel.Range col = (Excel.Range)xlWorkSheet.Columns["B:B", Type.Missing];
            excelApp.Visible = true;
            excelApp.ScreenUpdating = false;
            excelApp.DisplayAlerts = false;


        foreach (Excel.Range item in col.Cells)
        {
            string text = (string)item.Text;
            if (text == "A")
            {
              Console.Write("A was found in cell")
            }

Console.Write("This text will loop infinitely because it's iterating over every cell in Column B, not just the one with data. I want to stop this so I can do other things here and it's not just iterating forever") 
}

EDIT: I did not see that this was for C# when I read the question, so I'm not sure if this is applicable. The code is in VBA for Excel.

First, count the number of non-blank cells in the column you're looking at. Then, iterate over the cells in that column, decrementing that count every time you have a cell with data in it. Once you hit zero, that's the last cell. Code example below:

Dim int_cell_count As Integer
Dim int_row As Integer: int_row = 0

int_cell_count = Application.WorksheetFunction.CountA(Range("B:B"))
Do
    int_row = int_row + 1
    If Not IsEmpty(Range("B" & int_row)) Then
        int_cell_count = int_cell_count - 1
        ' do stuff with the cell here
    End If
Loop Until int_cell_count = 0

MsgBox "Last Cell was: B" & int_row

I figured it out and used Range.Special 's parameter type .xlCellTypeLastCell to find the last used cell in the whole sheet and then used .Row to find what row that was.. then concatenated the "B" column to that row number to get the last used cell in Row B. New code:

 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open("X:\\Private\\DATA\\PROJECT DATA\\Database\\TETRA\\Master Captured List - TETRA.xlsx");
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)excelWorkbook.Sheets[sheetSpaces];
            excelApp.Visible = true;
            excelApp.ScreenUpdating = false;
            excelApp.DisplayAlerts = false;
            Excel.Range last = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            int lastUsedRow = last.Row;
            string lastCell = "B" + lastUsedRow.ToString();
            Excel.Range range = xlWorkSheet.get_Range("B1", lastCell);

            foreach (Excel.Range item in range.Cells)
            {
                string text = (string)item.Text;
                if (text == "A")
                {
                  Console.Write("A was found in cell")
                }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM