Logic Error in Nested If Then Statements

goss

Active Member
Joined
Feb 2, 2004
Messages
372
Hi all,

Using Excel 2010.

I extracted data from .pdf to Excel using Able2Extract
Now I need to scrub the output a bit.

I see commonalities in the data for the start and stop of each set of data that I can key in on.

Once I find the start and stop points for each set of data I would like to fill all rows in-between the points and then discard anything that remains outside of these boundaries.

I have something wrong in my logic, way too many rows are deleted.
Does anyone else see the problem in my logic?

Code:
Option Explicit

Sub GetLineSets()
    'Purpose: Identify relevant line sets, delete all other rows
    Dim wbBook As Workbook
    Dim wsData As Worksheet
    Dim strFormula As String
    Dim lngRows As Long
    Dim C As Range
    Dim blnFlag As Boolean
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
    
    Set wbBook = ThisWorkbook
    Set wsData = wbBook.Worksheets("Data")
    blnFlag = False

    lngRows = wsData.Range("A65536").End(xlUp).Row
    strFormula = "=IF(AND(ISERROR(FIND(""INVOICE NUMBER"",F1)),ISERROR(FIND(""INVOICE TOTAL"",C1))),0,""INVOICE"")"
     
    With wsData
        .Range("A1").EntireColumn.Insert
        .Range("A1:A" & lngRows).Formula = strFormula
        
        For Each C In .Range("A1:A" & lngRows)
            C.Value = C.Value
        Next C
        
        Do While lngRows >= 1
            If blnFlag = False Then
                If .Cells(lngRows, 1).Value = 0 Then
                    .Cells(lngRows, 1).EntireRow.Delete
                End If
            Else
                If .Cells(lngRows, 1).Value = "Department" Then
                    blnFlag = True
                Else
                    If .Cells(lngRows, 1).Value = 0 Then
                        .Cells(lngRows, 1).Value = "Department"
                    Else
                        .Cells(lngRows, 1).EntireRow.Delete
                    End If
                End If
            End If
            lngRows = lngRows - 1 'Decrement counter
        Loop
    End With
    
    'Tidy Up
        Set wbBook = Nothing
        Set wsData = Nothing
        Set C = Nothing

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .Calculation = xlCalculationAutomatic
    End With
     
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The accepted method when deleting rows is to work backwards. Something like this:

Code:
       [COLOR=Red] [B]For i = lngRows To 1 Step -1[/B][/COLOR]
            If blnFlag = False Then
                If .Cells(lngRows, 1).Value = 0 Then
                    .Cells(lngRows, 1).EntireRow.Delete
                End If
            Else
                If .Cells(lngRows, 1).Value = "Department" Then
                    blnFlag = True
                Else
                    If .Cells(lngRows, 1).Value = 0 Then
                        .Cells(lngRows, 1).Value = "Department"
                    Else
                        .Cells(lngRows, 1).EntireRow.Delete
                    End If
                End If
            End If
            lngRows = lngRows - 1 'Decrement counter
        [COLOR=Red][B]Next i[/B][/COLOR]
 
Upvote 0
Is the worksheet formula working as expected?

Have you considered not using it?

You are already looping through the rows twice, why not loop through them once and check if the data should be deleted.

Not sure what you are checking for but there are one or two VBA functions you might be able to use.

In fact in the 2nd loop you seem to be doing a check in the code, why not extend that?
 
Upvote 0
Thanks Neil/Norie,

I took another look at the data and thought of a slightly different process using a couple of helper columns. Not as elegant..but easier for me to understand.:)

thx
w

Final:
Code:
Option Explicit

Sub GetLineSets()
    'Purpose: Identify relevant line sets to invoices, delete all other rows
    Dim wbBook As Workbook
    Dim wsData As Worksheet
    Dim C As Range
    Dim myRange As Range
    Dim strFormula As String
    Dim strFormula2 As String
    Dim strFormula3 As String
    Dim myValue As String
    Dim lngRows As Long
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
    
    Set wbBook = ThisWorkbook
    Set wsData = wbBook.Worksheets("Data")
    myValue = 0

    lngRows = wsData.Range("B65536").End(xlUp).Row
    strFormula = "=IF(ISERROR(FIND(""Department Number"",H1)),0,""Department"")"
    strFormula2 = "=IF(ISERROR(FIND(""Department Total"",E1)),0,""Department Total"")"
    strFormula3 = "=IF(ISTEXT(A1),A1,IF(ISTEXT(B1),B1,0))"
     
    With wsData
        .Range("A1:C1").EntireColumn.Insert
        .Range("A1:A" & lngRows).Formula = strFormula
        .Range("B1:B" & lngRows).Formula = strFormula2
        .Range("C1:C" & lngRows).Formula = strFormula3
        
        For Each C In .Range("A1:C" & lngRows)
            C.Value = C.Value
        Next C

        .Range("A1:B1").EntireColumn.Delete

        Set myRange = .Range("A1:A" & lngRows)

        For Each C In myRange
            If C.Value = "Department Total" Then
                C.Value = "Department Total"
                myValue = 0
            Else
                If C.Value = 0 Then
                    C.Value = myValue
                Else
                    myValue = C.Value
                End If
            End If
        Next C
        
        'Delete non-relevant rows
            Do While lngRows >= 1
                If .Cells(lngRows, 1).Value = 0 Then
                    .Cells(lngRows, 1).EntireRow.Delete
                End If
                lngRows = lngRows - 1
            Loop
            
    End With
    
    'Tidy Up
        Set wbBook = Nothing
        Set wsData = Nothing
        Set C = Nothing
        Set myRange = Nothing

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .Calculation = xlCalculationAutomatic
    End With
     
End Sub
 
Upvote 0
You could probably speed the code up a bit by either not changing the formulas to values or doing it in one go.
Code:
.Range("A1:C" & lngRows).Value = .Range("A1:C" & lngRows).Value
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top