VBA Faster Code

ziad alsayed

Well-known Member
Joined
Jul 17, 2010
Messages
665
dear all

the below code is looping over 20000 row , and it is taking a bit of time .
please advise how can i make it faster

appreciate any help

Code:
For i = 2 To finalrow
Cells(i, 4).FormulaR1C1 = "=LEFT(RC[-1],1)"
Cells(i, 5).FormulaR1C1 = "=IF(RC[-1]=""1"",""PHC"",IF(RC[-1]=""2"",""Apapa"",IF(RC[-1]=""3"",""VI"",IF(RC[-1]=""4"",""Kano"",IF(RC[-1]=""5"",""Abuja"",IF(RC[-1]=""6"",""Ikeja"",""""))))))"
Next i
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Make sure you are using Application.ScreenUpdating = False

Also, since you are putting in formulas, consider turning off calculations as well and then turning the calculations back on after it is done putting them in:

Code:
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
For i = 2 To finalrow
    Cells(i, 4).FormulaR1C1 = "=LEFT(RC[-1],1)"
    Cells(i, 5).FormulaR1C1 = "=IF(RC[-1]=""1"",""PHC"",IF(RC[-1]=""2"",""Apapa"",IF(RC[-1]=""3"",""VI"",IF(RC[-1]=""4"",""Kano"",IF(RC[-1]=""5"",""Abuja"",IF(RC[-1]=""6"",""Ikeja"",""""))))))"
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
 
Upvote 0
Try

Code:
Application.Calculation = xlCalculationManual
For i = 2 To finalrow
    Cells(i, 4).FormulaR1C1 = "=LEFT(RC[-1],1)"
    Cells(i, 5).FormulaR1C1 = "=IF(RC[-1]=""1"",""PHC"",IF(RC[-1]=""2"",""Apapa"",IF(RC[-1]=""3"",""VI"",IF(RC[-1]=""4"",""Kano"",IF(RC[-1]=""5"",""Abuja"",IF(RC[-1]=""6"",""Ikeja"",""""))))))"
Next i
Application.Calculation = xlCalculationAutomatic
Calculate
 
Upvote 0
try this

Code:
Sub add_formula()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Cells(2, 4).FormulaR1C1 = "=LEFT(RC[-1],1)"
Cells(2, 5).FormulaR1C1 = "=IF(RC[-1]=""1"",""PHC"",IF(RC[-1]=""2"",""Apapa"",IF(RC[-1]=""3"",""VI"",IF(RC[-1]=""4"",""Kano"",IF(RC[-1]=""5"",""Abuja"",IF(RC[-1]=""6"",""Ikeja"",""""))))))"
Range("D2").AutoFill Destination:=Range("D2:D" & LR)
Range("E2").AutoFill Destination:=Range("E2:E" & LR)
End Sub
 
Upvote 0
I'd create a table with of 6 rows by 2 columns, first column with numbers 1-6 and second columns with the associated values. Then use a VLOOKUP to reference and return what you want.

This may also be faster, but I've used absolute references, rather than relative:
Rich (BB code):
Application.ScreenUpdating = False
With Range("D2:D" & finalrow)
  .Formula = "=VLOOKUP(LEFT(C2,1),$E$1:$F$6,2,0)"
  .FillDown
End With
Application.ScreenUpdating = True
Red bit is range of where ever your table is
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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