我有这个当前函数,该函数在遍历表时清除第2列的值等于-1的行。我遇到的问题是我不想检查表中的最后一行。我该怎么做呢?
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.
}
我正在尝试将旧的VBA代码转换为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
因此,我尝试将其定义为后缀“ MoveEmUp”
被叫。
最佳答案
用于循环。 Count属性将返回集合中的行数,您将count-2指定为从0到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];
}