So I'm new to VBA and am having problems with this one marco. I have written it in two different ways to get it to work and I cant. Both the codes work but just take about 10 mins and sometimes it crashes. I am trying to identify the last time an item was moved in our inventory by using the transaction data of the last 2 years. For reference FinalRowInput is about 21,000 and FinalRowOutput is 102. These are the two codes:
Sub DateSearch()
Dim NewDate As Date
Dim OldDate As Date
Dim SKU As String
Dim i As Integer
Dim j As Integer
Sheets("Output").Range("E2:E500").ClearContents
FinalRowInput = Sheets("Input").Range("A40000").End(xlUp).Row
FinalRowOutput = Sheets("Output").Range("A100000").End(xlUp).Row
j = 268
Do While j < FinalRowInput + 1
Sheets("Input").Select
SKU = Cells(j, 11)
MoveDate = Cells(j, 1).Value
i = 2
Do While i < FinalRowOutput + 1
Sheets("Output").Select
If SKU = Cells(i, 1) Then
If Cells(i, 5) = 0 Then
Cells(i, 5) = MoveDate
i = FinalRowOutput
End If
If MoveDate > Cells(i, 5) Then
Cells(i, 5) = MoveDate
i = FinalRowOutput
End If
End If
i = i + 1
Loop
j = j + 1
Loop
End Sub
and
Sub DateSearch()
Dim NewDate As Date
Dim OldDate As Date
Dim SKU As String
Dim i As Integer
Dim j As Integer
Sheets("Output").Range("E2:E50").ClearContents
FinalRowInput = Sheets("Input").Range("A40000").End(xlUp).Row
FinalRowOutput = Sheets("Output").Range("A100000").End(xlUp).Row
j = 2
Do While j < FinalRowOutput + 1
Sheets("Output").Select
SKU = Cells(j, 1)
OldDate = 1 / 12 / 1999
i = 2
Do While i < FinalRowInput + 1
Sheets("Input").Select
If SKU = Cells(i, 11) Then
NewDate = Cells(i, 1).Value
If NewDate > OldDate Then
Sheets("Output").Select
Cells(j, 5) = NewDate
OldDate = NewDate
End If
End If
i = i + 1
Loop
j = j + 1
Loop
End Sub
Sub DateSearch()
Dim NewDate As Date
Dim OldDate As Date
Dim SKU As String
Dim i As Integer
Dim j As Integer
Sheets("Output").Range("E2:E500").ClearContents
FinalRowInput = Sheets("Input").Range("A40000").End(xlUp).Row
FinalRowOutput = Sheets("Output").Range("A100000").End(xlUp).Row
j = 268
Do While j < FinalRowInput + 1
Sheets("Input").Select
SKU = Cells(j, 11)
MoveDate = Cells(j, 1).Value
i = 2
Do While i < FinalRowOutput + 1
Sheets("Output").Select
If SKU = Cells(i, 1) Then
If Cells(i, 5) = 0 Then
Cells(i, 5) = MoveDate
i = FinalRowOutput
End If
If MoveDate > Cells(i, 5) Then
Cells(i, 5) = MoveDate
i = FinalRowOutput
End If
End If
i = i + 1
Loop
j = j + 1
Loop
End Sub
and
Sub DateSearch()
Dim NewDate As Date
Dim OldDate As Date
Dim SKU As String
Dim i As Integer
Dim j As Integer
Sheets("Output").Range("E2:E50").ClearContents
FinalRowInput = Sheets("Input").Range("A40000").End(xlUp).Row
FinalRowOutput = Sheets("Output").Range("A100000").End(xlUp).Row
j = 2
Do While j < FinalRowOutput + 1
Sheets("Output").Select
SKU = Cells(j, 1)
OldDate = 1 / 12 / 1999
i = 2
Do While i < FinalRowInput + 1
Sheets("Input").Select
If SKU = Cells(i, 11) Then
NewDate = Cells(i, 1).Value
If NewDate > OldDate Then
Sheets("Output").Select
Cells(j, 5) = NewDate
OldDate = NewDate
End If
End If
i = i + 1
Loop
j = j + 1
Loop
End Sub
Last edited: