Need Help with Speed of VBA Code that works, but...

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am running Excel 2007. I am a beginning to intermediate VBA programmer....

I have a large spreadsheet with over 2 years of data in it. It has over 155,000 rows. Because of different users, some of the data entry has different entries in column O than what it should be. The data in column O correponds to a part number in column P. My job is to clean the data up.

I wrote a quick do loop statement to search for a part number in column P, check the corresponding entry in column O and change it if needed.

The code works, but it takes almost 25 minutes to work through the spreadsheet in columns P:O one time (all 155,000 rows).

Can anyone help me speed this up?

Here is my do loop code:

Code:
Sub Correct_Data()

        Sheets("Data").Select
        Range("P2").Select
    Do
        If Selection = "AREC06099" Then
            ActiveCell.Offset(rowOffset:=0, columnOffset:=-1).Activate
            ActiveCell.Formula = "CAP .001UF CM 50V 5% C0G 0402"
            ActiveCell.Offset(rowOffset:=1, columnOffset:=1).Activate
        End If
            ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
    Loop Until ActiveCell = ""

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
at the beginning
Code:
application.screenupdating = false
and at the end
Code:
application.screenupdating = true

your screen will stop updating, but your code will be SO much faster.
 
Upvote 0
2 things

1. you don't need to 'select' a cell to determine or effect its contents
2. you don't need to look at every cell to 'find' a particular value

Here's a bit of code that might help
Code:
Sub correct2()
    Application.ScreenUpdating = 0
    With Sheets("data").Columns(16)
        Set f = .Find(what:="AREC06099")
        If Not f Is Nothing Then
            fs = f.Address
            Do
                f.Offset(, -1) = "CAP .001UF CM 50V 5% C0G 0402"
                Set f = .FindNext(f)
            Loop While fs <> f.Address
        End If
    End With
End Sub
 
Last edited:
Upvote 0
This uses autofilter to filter column P on "AREC06099"
Then adds to column O "CAP .001UF CM 50V 5% C0G 0402" to just the visible filtered cells.

Code:
Sub Correct_Data2()

    Dim strItemP As String, strItemO As String
    
    strItemP = "AREC06099"
    strItemO = "CAP .001UF CM 50V 5% C0G 0402"

    Sheets("Data").Select
    
    Application.ScreenUpdating = False
    
    
    If Not Range("P:P").Find(strItemP, , , xlWhole, , , False) Is Nothing Then
    
        Columns("P").AutoFilter Field:=1, Criteria1:=strItemP
        
        Range("P2", Range("P" & Rows.Count).End(xlUp)). _
            SpecialCells(xlCellTypeVisible).Offset(, -1).Value = strItemO
                
        ActiveSheet.AutoFilterMode = False
                
    Else
        MsgBox "No match found for """ & strItemP & """"
    End If
    
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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