VBA Code to Insert Columns & Formula Incredibly Slow - What can be done to make more efficient?

aeg565

New Member
Joined
Apr 9, 2018
Messages
1
I'm using the code below to insert a new column to the right of an existing column (based on existing column's name). The code then inserts an equation from the second row to the last row.

The first half (with the vlookup) works fine. However the second half (with the int formula) causes the system to hang and can take up to 5-10 minutes to complete.

What can be done to make this code more efficient? Thanks in advance!

Sub VM_PO_InsertCol()Dim Lastrow As Long


Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False


Lastrow = Range("A" & Rows.Count).End(xlUp).Row


For i = 1 To 14
If Cells(1, i).Value = "Vendor account" Then
Cells(1, i + 1).EntireColumn.Insert
Cells(1, i + 1).Value = "Vendor Class"
Cells(1, i + 1).Interior.Color = RGB(155, 187, 89)
Cells(1, i + 1).EntireColumn.NumberFormat = "0.0"
For j = 1 + 1 To Lastrow
Cells(j, i + 1).Formula = "=IFERROR(vlookup([@[Vendor account]],'Vendor Mapping'!A:I,9,0),""Inactive"")"
Next j
ElseIf Cells(1, i).Value = "Created date and time" Then
Cells(1, i + 1).EntireColumn.Insert
Cells(1, i + 1).Value = "Created date and time (NO TS)"
Cells(1, i + 1).Interior.Color = RGB(155, 187, 89)
Cells(1, i + 1).EntireColumn.NumberFormat = "m/d/yyyy"
For j = 1 + 1 To Lastrow
Cells(j, i + 1).Formula = "=INT([@[Created date and time]])"
Next j
End If


Next i

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try turning off calculation while you are inserting new formulas.

Code:
    Application.Calculation = xlCalculationManual
    '
    ' insert new columns and formulas
    '
    Application.Calculation = xlCalculationAutomatic
 
Upvote 0
The below should run a bit faster as it writes the formula to the entire range rather than looping through each cell. It might be faster still if you only format the relevant cells rather than entire columns...

Code:
Sub VM_PO_InsertCol()
Dim Lastrow, i, j As Long

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .DisplayStatusBar = False
    .EnableEvents = False
    .DisplayPageBreaks = False
End With
Lastrow = Range("A" & Rows.Count).End(xlUp).row
 

 For i = 1 To 14
    With Cells(1, i)
        Select Case .Value
            Case "Vendor account":
            .Value = "Vendor Class"
            .Interior.Color = RGB(155, 187, 89)
            .EntireColumn.NumberFormat = "0.0"
            
            Range(Cells(2, 2), Cells(2, Lastrow)).Formula = "=IFERROR(vlookup([@[Vendor account]],'Vendor Mapping'!A:I,9,0),""Inactive"")"
        
            Case "Created date and time":
            .EntireColumn.Insert
            .Value = "Created date and time (NO TS)"
            .Interior.Color = RGB(155, 187, 89)
            .EntireColumn.NumberFormat = "m/d/yyyy"
    
            Range(Cells(2, 2), Cells(2, Lastrow)).Formula = "=INT([@[Created date and time]])"
        End Select
    End With
 Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .DisplayStatusBar = True
    .EnableEvents = True
    .DisplayPageBreaks = True
End With

 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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