ThrottleWorks
Excel Ninja
Hi I have a macro, this macro is not desgined me.
The macro takes around 40 minutes to run.
This is desgined by experts and I am not able to understand the code.
Can someone help me to reduce the running time of the macro please.
The macro does various calculations.
1 main condition used in the loop is cell has to be non-blank.
Do while loops are used in the macro.
At present I have 900 non-blank rows.
I have pasted the code used below.
Code with my comments
----------------------------------------------------------------------------------------
Sub Report_Sachin()
Application.ScreenUpdating = False
Windows("New.xlsm"
.Activate
Sheets("abc"
.Select
'Range D1
td = Cells(1, 4)
Windows("data.xlsx"
.Activate
Sheets("Data"
.Select
'Cells(1,7) is range G1
'if date in G1 <> range D1 - 1 in accrual sheet then
'Date in the cell G1 wll be replaced with Cell D1
If Cells(1, 7) <> td - 1 Then
MsgBox ("Pls Check the date"
End
Else: Cells(1, 7) = td
End If
Windows("New.xlsm"
.Activate
Sheets("abc"
.Select
'Cells(1,3) is range c1 from accrual sheet
'if this is = cells(1,4) i.e. range d1 then
If Cells(1, 3) = Cells(1, 4) Then
Windows("New.xlsm"
.Activate
Sheets("abc"
.Select
k = 4
'Cells(k,2) is range B4
'Do untill B4 is blank
Do Until Cells(k, 2) = ""
isin = Cells(k, 2)
acc = Cells(k, 4)
Windows("data.xlsx"
.Activate
Sheets("Data"
.Select
l = 3
'Cells(l,1) is range A3
'This line is checkign wheather currency is blank
'if cells(i,5) i.e. E3 is = isin value (isin here is a variable declared above)
'then update the value acc in column AV i.e. DI
'acc is calculated in the accrual sheet
Do Until Cells(l, 1) = ""
If Cells(l, 5) = isin Then
Cells(l, 48) = acc
Exit Do
End If
l = l + 1
Loop
k = k + 1
Windows("New.xlsm"
.Activate
Sheets("abc"
.Select
Loop
End If
Windows("data.xlsx"
.Activate
Sheets("Data"
.Select
'cell(j,1) in is Range A3
'Do untill cells in the column A are blank
j = 3
Do Until Cells(j, 1) = ""
'cells (j,21) is column U
'cells (j,23) is column W
'cells (j,22) is column V
Cells(j, 21) = Cells(j, 23)
Cells(j, 22) = Cells(j, 23)
'cells (j,52) is column AZ
'cells (j,52) is cells (j,52) + cells (j,48)
'cells (j,48) is column AV
'cells (j,56) is cells (j,56) + cells (j,48)
'cells (j,56) is column BD
Cells(j, 52) = Cells(j, 52) + Cells(j, 48)
Cells(j, 56) = Cells(j, 56) + Cells(j, 48)
'cells (j,49) is column AW
br />Cells(j, 49) = 0
j = j + 1
Windows("data.xlsx"
.Activate
Sheets("Data"
.Select
Loop
MsgBox "Done !"
Application.ScreenUpdating = True
End Sub
---------------------------------------------------------------------------------------
Clean code (without comments)
[pre]
[/pre]
----------------------------------------------------------------------------------------
The macro takes around 40 minutes to run.
This is desgined by experts and I am not able to understand the code.
Can someone help me to reduce the running time of the macro please.
The macro does various calculations.
1 main condition used in the loop is cell has to be non-blank.
Do while loops are used in the macro.
At present I have 900 non-blank rows.
I have pasted the code used below.
Code with my comments
----------------------------------------------------------------------------------------
Sub Report_Sachin()
Application.ScreenUpdating = False
Windows("New.xlsm"
Sheets("abc"
'Range D1
td = Cells(1, 4)
Windows("data.xlsx"
Sheets("Data"
'Cells(1,7) is range G1
'if date in G1 <> range D1 - 1 in accrual sheet then
'Date in the cell G1 wll be replaced with Cell D1
If Cells(1, 7) <> td - 1 Then
MsgBox ("Pls Check the date"
End
Else: Cells(1, 7) = td
End If
Windows("New.xlsm"
Sheets("abc"
'Cells(1,3) is range c1 from accrual sheet
'if this is = cells(1,4) i.e. range d1 then
If Cells(1, 3) = Cells(1, 4) Then
Windows("New.xlsm"
Sheets("abc"
k = 4
'Cells(k,2) is range B4
'Do untill B4 is blank
Do Until Cells(k, 2) = ""
isin = Cells(k, 2)
acc = Cells(k, 4)
Windows("data.xlsx"
Sheets("Data"
l = 3
'Cells(l,1) is range A3
'This line is checkign wheather currency is blank
'if cells(i,5) i.e. E3 is = isin value (isin here is a variable declared above)
'then update the value acc in column AV i.e. DI
'acc is calculated in the accrual sheet
Do Until Cells(l, 1) = ""
If Cells(l, 5) = isin Then
Cells(l, 48) = acc
Exit Do
End If
l = l + 1
Loop
k = k + 1
Windows("New.xlsm"
Sheets("abc"
Loop
End If
Windows("data.xlsx"
Sheets("Data"
'cell(j,1) in is Range A3
'Do untill cells in the column A are blank
j = 3
Do Until Cells(j, 1) = ""
'cells (j,21) is column U
'cells (j,23) is column W
'cells (j,22) is column V
Cells(j, 21) = Cells(j, 23)
Cells(j, 22) = Cells(j, 23)
'cells (j,52) is column AZ
'cells (j,52) is cells (j,52) + cells (j,48)
'cells (j,48) is column AV
'cells (j,56) is cells (j,56) + cells (j,48)
'cells (j,56) is column BD
Cells(j, 52) = Cells(j, 52) + Cells(j, 48)
Cells(j, 56) = Cells(j, 56) + Cells(j, 48)
'cells (j,49) is column AW
br />Cells(j, 49) = 0
j = j + 1
Windows("data.xlsx"
Sheets("Data"
Loop
MsgBox "Done !"
Application.ScreenUpdating = True
End Sub
---------------------------------------------------------------------------------------
Clean code (without comments)
[pre]
Code:
Sub Report_Sachin()
Application.ScreenUpdating = False
Windows("New.xlsm").Activate
Sheets("abc").Select
td = Cells(1, 4)
Windows("data.xlsx").Activate
Sheets("Data").Select
If Cells(1, 7) <> td - 1 Then
MsgBox ("Pls Check the date")
End
Else: Cells(1, 7) = td
End If
Windows("New.xlsm").Activate
Sheets("abc").Select
If Cells(1, 3) = Cells(1, 4) Then
Windows("New.xlsm").Activate
Sheets("abc").Select
k = 4
Do Until Cells(k, 2) = ""
isin = Cells(k, 2)
acc = Cells(k, 4)
Windows("data.xlsx").Activate
Sheets("Data").Select
l = 3
Do Until Cells(l, 1) = ""
If Cells(l, 5) = isin Then
Cells(l, 48) = acc
Exit Do
End If
l = l + 1
Loop
k = k + 1
Windows("New.xlsm").Activate
Sheets("abc").Select
Loop
End If
Windows("data.xlsx").Activate
Sheets("Data").Select
j = 3
Do Until Cells(j, 1) = ""
Cells(j, 21) = Cells(j, 23)
Cells(j, 22) = Cells(j, 23)
Cells(j, 52) = Cells(j, 52) + Cells(j, 48)
Cells(j, 56) = Cells(j, 56) + Cells(j, 48)
Cells(j, 49) = 0
j = j + 1
Windows("data.xlsx").Activate
Sheets("Data").Select
Loop
MsgBox "Done !"
Application.ScreenUpdating = True
End Sub
----------------------------------------------------------------------------------------