Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ListObject iterate through table besides for last row

I have this current function that clears a row where column 2 value equals -1 while iterating through the table. The issue that I am having is that I don't want to check the last row in the table. How can I go about of doing this?

foreach (Excel.ListRow lr in xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].ListRows)
{
    CurrentRow = CurrentRow + 1;
    if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[CurrentRow, 2].value == -1)
    {
        lr.Range.Clear();
        //CALL MoveEmUP
        //ListRow = ListRow - 1? I'd like to possibly do something like this.
    }

OLD VBA Code I'm trying to convert into C#

For CurrentRow = 1 To Tablesize
    If Lo.ListColumns("Column2").DataBodyRange(CurrentRow) = -1 Then
        Ros(CurrentRow).Range.Clear
        MoveEmUpOne Lo, CurrentRow, Tablesize
        Tablesize = Tablesize - 1 'table didn't really get smaller, but number of "real"rows decreased by 1
        CurrentRow = CurrentRow - 1 ' check this row again -- stuff from below might need cleard
    End If

So, I am trying to have it defined afterword "MoveEmUp" gets called.

like image 773
Brad Avatar asked Dec 13 '25 07:12

Brad


1 Answers

Use for loop. Count property will return number of rows in your collection, you specified count-2 to have it from 0 to lastOne-1.

var listRowsValue = WorkBook.Worksheets["Sheet1"].ListObjects["table1"].ListRows;

for(int i=0; i < listRowsValue.Count - 2; i++)
{
    //do your stuff; 

    var lr = listRowsValue[i];
}
like image 195
mybirthname Avatar answered Dec 15 '25 21:12

mybirthname



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!