Your first issue is that you are using a loop.
Never Ever Ever use a loop unless you really have to.
If you really have no other option keep the code inside the loop to a minimum and limit the number of times the macro goes around the loop.
Having said that I will now look at your code.
So you are starting at row 2 and looping to the first empty row in column A
You are looking at column D
If D = "Direct traffic" then Column J ="Direct" oftherwise it is indirect
Ok:
The Formula for Cell J2 is
Formula:
=IF(D2="Direct traffic","Direct", "Indirect")
So I Paste that formula into cell J2, Start the Macro Recorder, Select Cell J2, click inside the formula bar and press enter.
I then stop the macro recorder and look at the macro that it has recorded
Sub Macro6()
'
' Macro6 Macro
'
'
Range("J2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-6]=""Direct traffic"",""Direct"", ""Indirect"")"
End Sub
Before I can go any further I need to find the Last row.
So I need the line of code:
LR = Cells(rows.count,1).End(xlUp).Row
So I can now edit the recorded Macro to:
Sub Macro6()
LR = Cells(rows.count,1).End(xlUp).Row
Range("J2:J" & LR).FormulaR1C1 = _
"=IF(RC[-6]=""Direct traffic"",""Direct"", ""Indirect"")"
Range("J2:J" & LR).value = Range("J2:J" & LR).value
End Sub
Try that.
Instead of going around a loop 30000 times and performing 4 actions each time ie 120, 000 actions.
I have achieved the same result using 3 Actions, Guess which solution is faster?
Bookmarks