I'm encountering an issue using excel VBA macros where the execution portion of the code runs in excess of 8-10 minutes sometimes. I've narrowed it down to this part of the code that copies and pastes to another sheet based on the values of cells in a row.
Sub ChangeTest()
Sheets.Add.Name = "FY16"
Sheets.Add.Name = "FY17"
Sheets.Add.Name = "FY18"
Sheets.Add.Name = "FY19"
'Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("FY SalesLeads")
j = 1 ' Start copying to row 1 in target sheet
k = 1
l = 1
m = 1
For Each c In Source.Range("B1:B8000") ' Do 1000 rows
If c = "A" Then
Set Target = ActiveWorkbook.Worksheets("FY16")
Source.Rows(c.Row).Copy Target.Rows(j)
j = j + 1
ElseIf c = "B" Then
Set Target = ActiveWorkbook.Worksheets("FY17")
Source.Rows(c.Row).Copy Target.Rows(k)
k = k + 1
ElseIf c = "C" Then
Set Target = ActiveWorkbook.Worksheets("FY18")
Source.Rows(c.Row).Copy Target.Rows(l)
l = l + 1
ElseIf c = "D" Then
Set Target = ActiveWorkbook.Worksheets("FY19")
Source.Rows(c.Row).Copy Target.Rows(m)
m = m + 1
End If
Next c
End Sub
Is there a way to do this more efficiently that doesn't hang up Excel? I've also noticed that after running the Macro sometimes even Windows Explorer becomes unresponsive as well.
Thanks for what everyone does here, I love this community!