PoggiPJ
Active Member
- Joined
- Mar 25, 2008
- Messages
- 330
My spreadsheet is a contract file that includes a list of part numbers being sold. Each part number refers to a lookup table of ALL about 20,000 valid part numbers and prices. Once the contract is finalized, I want to make the contract sheet smaller by deleting all rows in the lookup table that are not required for this contract.
The following code works, but it takes more than 10 minutes to work through the complete list. I read down the lookup table. For each record in the lookup table, I call a routine that reads through an array of the part numbers that are included in this contract. If the lookup table part number IS included in the contract, I skip it. If it is NOT required, I delete it. I then return to the main lookup table and read in the next lookup table record.
This is the main routine where I progress down the big lookup table.
This is the routine that checks to see if the part is included in the contract.
I'm wondering if there is a better approach - or if there is a way to select the individual rows to be deleted, and then delete them all at once. Any ideas?
The following code works, but it takes more than 10 minutes to work through the complete list. I read down the lookup table. For each record in the lookup table, I call a routine that reads through an array of the part numbers that are included in this contract. If the lookup table part number IS included in the contract, I skip it. If it is NOT required, I delete it. I then return to the main lookup table and read in the next lookup table record.
This is the main routine where I progress down the big lookup table.
Code:
'Work down the Price File range from top to bottom
Set RefTableRange = DSWPrices.Range("DSWPriceRange")
RefTableIndex = 1
Application.Calculation = xlCalculationManual
While RefTableIndex < RefTableRange.Rows.Count
RefTableIndex = RefTableIndex + 1
'check if this part number is included in the contract
Call CheckRefTableRow(RefTableRange, RefTableIndex)
Wend
Code:
Private Sub CheckRefTableRow(ByRef RefTableRange As Range, ByRef RefTableRow As Long)
Dim ThisPartIsNotInTheContract As Boolean
Dim x As Long
'assumption that this part will NOT be in the contract and will delete it.
ThisPartIsNotInTheContract = True
While ThisPartIsNotInTheContract
For x = 1 To maxPAParts
If RefTableRange.Cells(RefTableRow, 1) = PAPartArray(x) Then
'the part actually IS in the contract
ThisPartIsNotInTheContract = False
Exit For
End If
Next x
If ThisPartIsNotInTheContract Then
'Since this part isn't included in the contract, delete it.
RefTableRange.Cells(RefTableRow, 1).EntireRow.Delete
deletedRecordCount = deletedRecordCount + 1
End If
Wend
End Sub