First of all, I´m a pretty new and ineffective VBA user, which you´ll definitely notice.
I have created a macro with loops that runs extremely slowly (about 10 minutes depending on the dataset, which differs in size every time) and I´m guessing that there´s a much better way of doing it than mine.
Basically, what I´m trying to do is automate a job that includes a lot of built-in functions in Excel. I got four columns and X amount of rows that need to be populated with formulas.
My idea was to calculate the formula for all four columns in row 1, then moving on to row 2 all the way to row X, using a simple "do loop". It looks something like this:
Range("j2").Select
rownumber = ActiveCell.Row
Do
'check if the cell on the left is empty to determine whether it´s the last row or not.
Range("J" & rownumber).Select
Range("J" & rownumber).Offset(0, -1).Select
If IsEmpty(ActiveCell) = True Then
Exit Do
Else
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _ "=INDEX(Sheet1!C[-4],MATCH(Sheet2!R[0]C[-6],Sheet1!C[-9],0))"
'next column
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _ "=INDEX(sheet1!C[-6],MATCH(sheet2!RC[-7],sheet1!C[-10],0))"
'next column
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _ "=INDEX(sheet3!C[-10],MATCH(sheet2!RC[-2],sheet1!C[-11],0))*sheet2!RC[-1]*sheet2!RC[-10]"
'next column
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _ "=IF(sheet2!RC[-12]=""BUY"",SUMIFS(sheet4!C[-7],sheet4!C[-12],sheet2!RC[-6],sheet4!C[-11],sheet2!RC[-9])+sheet2!RC[-11],SUMIFS(sheet4!C[-7],sheet4!C[-12],sheet2!RC[-6],sheet4!C[-11],sheet2!RC[-9])-sheet2!RC[-11])"
ActiveCell.Offset(0, 1).Select
rownumber = rownumber + 1
End If
Loop
This all works, but there must be a better solution that runs smoother. I understand that Excel needs to do lots of calculations with the nested if statements, but it would probably take me less than 10 minutes to do this manually, so I´m guessing it´s my code that slows things up.