I'm not sure how to optimize the code attached below into some form of loop and am hoping someone would be able to illustrate how best to tackle this.
Basically, I've inherited a spreadsheet with several VBA modules recorded from the macro recorder and/or written by someone inexperienced with VBA, and it's very slow to run. I've been going through and reducing a lot of redundant 'nested if' type sections into for loops in an attempt to optimize and speed things up, however I'm also very inexperienced and really not a coder myself as you can probably guess!
If Range("Link1").Value = "" Then
Application.CutCopyMode = False
GoTo Finale:
Else
If Range("Link2").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1")).Select
Selection.Copy
Else
If Range("Link3").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2")).Select
Selection.Copy
Else
If Range("Link4").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3")).Select
Selection.Copy
Else
If Range("Link5").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3", "Group4")).Select
Selection.Copy
Else
If Range("Link6").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3", "Group4", "Group5")).Select
Selection.Copy
Else
If Range("Link7").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3", "Group4", "Group5", "Group6")).Select
Selection.Copy
Else
If Range("Link8").Value = "" Then
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7")).Select
Selection.Copy
Else
ActiveSheet.Shapes.Range(Array("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7", "Group8")).Select
Selection.Copy
End If
End If
End If
End If
End If
End If
End If
End If
The code deals with copying 8 groups of 'things' (in this case, each containing text boxes and a graphic) and checks if a link has been populated, copying the previous groups when it finds an unpopulated link. The idea here is therefore that only populated groups are copied.
A second question regarding all this is that when you have multiple if statements like this, is it actually meaningfully faster or more optimal to reduce such things into loops, or should I be looking elsewhere to further optimize the spreadsheet? Turning long passages of recursive code into minimal loops certainly feels good(!), but I don't know if that's really what actually needs to be done to improve speed and stability or if it actually has little meaningful impact.