R
Ragnar Midtskogen
Hello,
I am working on a VB6 application that manipulates Excel Worksheets.
Right now I am working on code to delete rows identified with character 'r'
in column 1, and som of the wrong rows are deleted.
It looks as if Excel does nor recognize that rows have been deleted.
The code shown below is intended to delete the rows marked with 'r' and
leave the unmarked rows.
The sample I am using right now has column headers in row 1, as hown in the
small sample below, only the first 4 columns are shown:
The sheets contains only text, no formulas
Processed UserID
PolicyNumber Defendant
r FD3F55F4-D17D-4467-B45E-4008D295FE00 1212221 Dr.
Jones
r FD3F55F4-D17D-4467-B45E-4008D295FE00 1121222 Dr.
Hayes
r FD3F55F4-D17D-4467-B45E-4008D295FE00
r FD3F55F4-D17D-4467-B45E-4008D295FE00
r FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
The code in question is:
Dim oCommonExcelWorkbook As Excel.WorkBook
Dim oCurrentSheet As Excel.WorkSheet
All variable starting with ln are declared as Long
For lnRowIndex = lnLastSourceRow To 1 Step -1
If (oCurrentSheet.Cells(lnRowIndex, 1) = "Processed") Then
Else
If (oCurrentSheet.Cells(lnRowIndex, 1) = "r") Then
oCurrentSheet.Activate
Range(Cells(lnSourceRow, 1), Cells(lnSourceRow,
lnColumnCount)).Select
Selection.Delete
oCommonExcelWorkbook.Save
End If
End If
Next lnRowIndex
The lnLastSourceRow holds the number of the last row with data in it, in
this case 9.
the lnColumnCount holds the number of columns with data in them, in this
case 15.
I chose to run the loop index backwards to avoid tripping up Excel with
respect to the row numbers, but it appears to get tripped up anyway.
The result of running the code, verified by stepping through, is that rows
9, 8, and 7 are skipped, as should happen, then row number 6 is deleted.
As the index passes theough 5, 4, 3, and 2, the 'r' is detected and the row
is deleted, everything looks fine.
However, at the end of the loop, the sheet, when viewed in Excel, shows that
the original rows 6 through 9 have been deleted, the original rows 1 through
5 are still there.
Am I missing something here?
Any help would be appreciated, this is driving me to distraction.
Ragnar
I am working on a VB6 application that manipulates Excel Worksheets.
Right now I am working on code to delete rows identified with character 'r'
in column 1, and som of the wrong rows are deleted.
It looks as if Excel does nor recognize that rows have been deleted.
The code shown below is intended to delete the rows marked with 'r' and
leave the unmarked rows.
The sample I am using right now has column headers in row 1, as hown in the
small sample below, only the first 4 columns are shown:
The sheets contains only text, no formulas
Processed UserID
PolicyNumber Defendant
r FD3F55F4-D17D-4467-B45E-4008D295FE00 1212221 Dr.
Jones
r FD3F55F4-D17D-4467-B45E-4008D295FE00 1121222 Dr.
Hayes
r FD3F55F4-D17D-4467-B45E-4008D295FE00
r FD3F55F4-D17D-4467-B45E-4008D295FE00
r FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
FD3F55F4-D17D-4467-B45E-4008D295FE00
The code in question is:
Dim oCommonExcelWorkbook As Excel.WorkBook
Dim oCurrentSheet As Excel.WorkSheet
All variable starting with ln are declared as Long
For lnRowIndex = lnLastSourceRow To 1 Step -1
If (oCurrentSheet.Cells(lnRowIndex, 1) = "Processed") Then
Else
If (oCurrentSheet.Cells(lnRowIndex, 1) = "r") Then
oCurrentSheet.Activate
Range(Cells(lnSourceRow, 1), Cells(lnSourceRow,
lnColumnCount)).Select
Selection.Delete
oCommonExcelWorkbook.Save
End If
End If
Next lnRowIndex
The lnLastSourceRow holds the number of the last row with data in it, in
this case 9.
the lnColumnCount holds the number of columns with data in them, in this
case 15.
I chose to run the loop index backwards to avoid tripping up Excel with
respect to the row numbers, but it appears to get tripped up anyway.
The result of running the code, verified by stepping through, is that rows
9, 8, and 7 are skipped, as should happen, then row number 6 is deleted.
As the index passes theough 5, 4, 3, and 2, the 'r' is detected and the row
is deleted, everything looks fine.
However, at the end of the loop, the sheet, when viewed in Excel, shows that
the original rows 6 through 9 have been deleted, the original rows 1 through
5 are still there.
Am I missing something here?
Any help would be appreciated, this is driving me to distraction.
Ragnar