Hi all:
I have this code belove to find and copy data then caculate value, this code work fine but I thinks it's very slow.
Please help me make code run faster a have another way to do (find data, copy then cacutale)
Thanks./.
I have this code belove to find and copy data then caculate value, this code work fine but I thinks it's very slow.
Please help me make code run faster a have another way to do (find data, copy then cacutale)
Thanks./.
Code:
Sub t_All()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
Dim r As Integer
Set sh1 = Sheets("Data_new")
Set sh2 = Sheets("higher")
Set sh3 = Sheets("lower")
Set sh4 = Sheets("data_old")
r = sh2.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
rr = sh3.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
sh2.Cells.Clear
sh3.Cells.Clear
Application.ScreenUpdating = False
With sh1
For i = 3 To .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
For j = 21 To .Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
If .Cells(i, "M").Value + .Cells(i, "N").Value < .Cells(i, j).Value Then
sh2.Cells(Rows.Count, 1).End(xlUp)(2) = .Cells(i, 3).Value
sh2.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = .Cells(2, j).Value
sh2.Cells(Rows.Count, 1).End(xlUp).Offset(, 2) = .Cells(i, j).Value
ElseIf .Cells(i, "M").Value - .Cells(i, "N").Value > .Cells(i, j).Value Then
sh3.Cells(Rows.Count, 1).End(xlUp)(2) = .Cells(i, 3).Value
sh3.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = .Cells(2, j).Value
sh3.Cells(Rows.Count, 1).End(xlUp).Offset(, 2) = .Cells(i, j).Value
End If
Next
Next
End With
With sh4
For x = 3 To .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
For y = 21 To .Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
For ii = 2 To r
If sh2.Cells(ii, "A").Value = .Cells(x, "C").Value And sh2.Cells(ii, "B").Value = .Cells(2, y) Then
sh2.Cells(ii, "D").Value = .Cells(x, y).Value
End If
Next
For ii = 2 To rr
If sh3.Cells(ii, "A").Value = .Cells(x, "C").Value And sh3.Cells(ii, "B").Value = .Cells(2, y) Then
sh3.Cells(ii, "D").Value = .Cells(x, y).Value
End If
Next
Next
Next
End With
With sh2
For i = 2 To .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
.Cells(i, "E").Value = .Cells(i, "C").Value - .Cells(i, "D").Value
If .Cells(i, "D").Value = 0 Then
.Cells(i, "F").Value = 1
Else
.Cells(i, "F").Value = (.Cells(i, "E") / .Cells(i, "D"))
End If
Next
.Columns("F").NumberFormat = "#,###.##%"
.Columns("C:E").NumberFormat = "#,###"
End With
With sh3
For i = 2 To .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
.Cells(i, "E").Value = .Cells(i, "C").Value - .Cells(i, "D").Value
If .Cells(i, "D").Value = 0 Then
.Cells(i, "F").Value = 1
Else
.Cells(i, "F").Value = (.Cells(i, "E") / .Cells(i, "D"))
End If
Next
.Columns("F").NumberFormat = "#,###.##%"
.Columns("C:E").NumberFormat = "#,###"
End With
Application.ScreenUpdating = True
End Sub